This question already has answers here:
editText get text kotlin
(8 answers)
Closed last year.
I have the problem when I want to register a new E-Mail to my firebase store.
I have that button in my activity_register.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RegisterActivity">
<EditText
android:id="#+id/editTextEmailAddress"
android:layout_width="237dp"
android:layout_height="49dp"
android:layout_marginTop="104dp"
android:layout_marginBottom="12dp"
android:background="#drawable/edittextbackground"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
android:padding="10dp"
android:textColor="#color/black"
app:layout_constraintBottom_toTopOf="#+id/editTextPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
....
After that I want to use it to register to the firebase store:
fun register(view: View){
val email: String=R.id.editTextEmailAddress.toString()
val password=R.id.editTextPassword.javaClass.toString()
auth.createUserWithEmailAndPassword(email,password).addOnCompleteListener { task ->
if(task.isSuccessful){
val intent= Intent(this,MainActivity::class.java)
startActivity(intent)
finish()
}
}.addOnFailureListener { exception ->
Toast.makeText(applicationContext,exception.localizedMessage,Toast.LENGTH_LONG).show()
}
}
If I take a look at the debugger there I can see the following:
Screenshot from Debugger
It looks like it is an integer. How can I fix that Problem?
I tried to solve it with bindings, but there the inflate is not possible.
Same Problem with the password one row below.
Problem is that you are taking the reference to the view instead, you should do like this
val emailField = findViewById(R.id.editTextEmailAddress)
val passwordField = findViewById(R.id.editTextPassword)
Now access the text
val email = emailField.text.toString()
val password = passwordField.text.toString()
Related
ok, brand new to android studio and kotlin (not programming in general)
installed Android Studio, set up a couple of hardware profiles (and enabled dev mode).
Here is my issue:
Simple app, 'every time you click the button, the number above doubles.'
in the first_fragment.xml file, I rename the ID to 'textDisplayedValue'
in the MainActivity.kt file, I create 2 variables originalValue and newValue.
When I try to set the originalValue to the value in textDisplayedValue, I get an 'Unresolved reference' error
Code
Log
In addition, when I launch the debugger, the 'ok' button is disabled. I assume that is due to the code errors so correct me if I am wrong.
Thanks in advance for the help.
Layout File
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstFragment">
<TextView
android:id="#+id/textDisplayedValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
app:layout_constraintBottom_toTopOf="#id/button_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textDisplayedValue" />
</androidx.constraintlayout.widget.ConstraintLayout>
binding.fab.setOnClickListener { view ->
val originalValue = 1
val newValue = originalValue * 2
textDisplayedValue = newValue.toString()
Snackbar.make(view, "Value $originalValue changed to $newValue", Snackbar.LENGTH_LONG).show()
}
Try this
Comment the not working code
From the toolbar select
A. Build -> clean project
B. After that Build -> rebuild project
Uncomment the not working code and do this
val originalValue = binding.textDisplayedValue. text() .toString().toLong()
val newValue = originalValue * 2
as you can see from the image above, there is something like a toast with text "Frequently used email" . whenever I complete typing an email then that toast will appear, I don't think I make it programmatically. It seems it appears only on Android 10, in my Android Lollipop that toast doesn't appear. I use Xiaomi Redmi Note 7 for my Android 10.
I want to remove that toast from my edittext. how to do that ?
here is the XML for the edittext
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/email_login_textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:theme="#style/TextInputLayoutAppearance"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView_back_button">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/email_textView_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/email"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
and in Kotlin code, I just have this listener
emailTextView.setOnFocusChangeListener { v, hasFocus ->
val emailText = emailTextView.text.toString()
if (!emailText.isEmpty()) {
if (!hasFocus && (!emailText.contains("#") || (!emailText.contains(".")))) {
emailTextInputLayout.isErrorEnabled = true
emailTextInputLayout.error = "wrong email format"
} else if (!hasFocus) {
emailTextInputLayout.isErrorEnabled = false
emailTextInputLayout.error = ""
}
}
}
I have tried to change/remove android:inputType="textEmailAddress" , but that toast is still appear
I was also facing this problem and have solved by adding the following -
android:inputType="textNoSuggestions|textEmailAddress"
textNoSuggestions will prevent the 'frequently used email' toast and since the EditText is for email address so you need to add textEmailAddress as well
The Code A and Code B are from the project https://github.com/android/databinding-samples.
The Code B display an icon based fun popularityIcon(view: ImageView, popularity: Popularity) and works well.
I find that project can still work well even if I rename #BindingAdapter("app:popularityIcon") to #BindingAdapter("myok:popularityIcon"), just like Code C, why?
Code A
object BindingAdapters {
#BindingAdapter("app:popularityIcon")
#JvmStatic fun popularityIcon(view: ImageView, popularity: Popularity) {
val color = getAssociatedColor(popularity, view.context)
ImageViewCompat.setImageTintList(view, ColorStateList.valueOf(color))
view.setImageDrawable(getDrawablePopularity(popularity, view.context))
}
...
}
Code B
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginTop="24dp"
android:contentDescription="#string/profile_avatar_cd"
android:minHeight="48dp"
android:minWidth="48dp"
app:layout_constraintBottom_toTopOf="#+id/likes_label"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintVertical_chainStyle="packed"
app:popularityIcon="#{viewmodel.popularity}"/>
Code C
object BindingAdapters {
#BindingAdapter("myok:popularityIcon")
#JvmStatic fun popularityIcon(view: ImageView, popularity: Popularity) {
val color = getAssociatedColor(popularity, view.context)
ImageViewCompat.setImageTintList(view, ColorStateList.valueOf(color))
view.setImageDrawable(getDrawablePopularity(popularity, view.context))
}
...
}
Databinding ignores namespaces. So it removes app: or myok: or anything else. Also, if you put both adapters with the same name but different namespaces, you would get an error telling you that there are more than one adapter for popularityIcon.
You can check the docs for more information.
Note: The Data Binding Library ignores custom namespaces for matching purposes.
you need to update your namespace in your XML were you using this binding.
like below
xmlns:myok="http://schemas.android.com/apk/res-auto"
check below code
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myok="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</androidx.constraintlayout.widget.ConstraintLayout>
I have 2 button and 1 textview in my layout. I had just trained, but saw some bugs. When users starts the cronometer they can unlimited click on the start button how can i fix it ?
I'm really newbie in the android development. Can you give advices for me, while i was watching my udemy lesson, i could catch the codes and basics. After that, when i try to build some basic projects for improve my skills, it gets hard. I have many syntax errors. I know what to do, but I'm confusing the order.
What can i do to do get better ?
main_activity.kt
var runnable : Runnable = Runnable { }
var handler : Handler = Handler( )
var number = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Cronometer have bugs
// User can click the start button for unlimited
}
fun startTime(view: View) {
runnable = object : Runnable {
override fun run() {
number++
textView2.text = "$number"
handler.postDelayed(this,1000)
}
}
handler.post(runnable)
}
fun stopTime(view:View) {
handler.removeCallbacks(runnable)
}
}
```
activity_main.xml
```<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:onClick="startTime"
android:text="START"
app:layout_constraintBaseline_toBaselineOf="#+id/button4"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="49dp"
android:layout_marginTop="88dp"
android:onClick="stopTime"
android:text="STOP"
app:layout_constraintStart_toEndOf="#+id/button3"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="180dp"
android:layout_height="40dp"
android:layout_marginTop="109dp"
android:gravity="center"
android:text="0"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
Your question is not clear. If you want to prevent the user from clicking on the buttons more than once, you could disable them as soon as a click event is detected. For example, in your startTime method you could add
(view as Button).enabled = false
You need to cast your view to Button because the enabled property is not present in the View class (it comes from the TextView class actually, but Button inherits from it).
Then, you need to enable it again when you click on the stop button. You will need to retrieve a reference to your start button somehow, so you can access and enable it from your stopTime method. I'll leave that as an exercise for you ;).
This seems to work on Android 6 Marshmallow device Xiaomi Redmi 3S.
Seems like an emulator problem, but it isn't really misbehaving in any other sense.
I am a beginner in Android development and don't know much.
I have already confirmed that notifications for my app is ON.
Specs:
API Level : Android 4.4 (KitKat)
Android Studio version: 3.6.4
Emulator: Pixel XL API 28
All I attempted to do was a toast message would pop up telling how many times the button has been clicked.
Here's the MainActivity.kt:
class MainActivity : AppCompatActivity() {
private var greetingCount = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnToastMessage.setOnClickListener {
val suffix = when (++greetingCount) {
1 -> "st"
2 -> "nd"
3 -> "rd"
else -> "th"
}
val greetingMsg = "Welcome for the $greetingCount$suffix time(s)!"
Toast.makeText(this#MainActivity, greetingMsg, Toast.LENGTH_LONG).show()
Log.i(".MainActivity", "Clicked Button")
}
}
}
And here's the activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="30dp">
<TextView
android:id="#+id/txtHelloWorld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.314" />
<Button
android:id="#+id/btnToastMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtHelloWorld"
app:layout_constraintVertical_bias="0.49" />
</androidx.constraintlayout.widget.ConstraintLayout>
Didn't know this could be done, but wiping data and cold-booting the device fixed it.
Following are the steps you can follow (because I too got stuck):
Try restarting and cleaning your emulator device.
OR
You can run the app on your physical (android) device by setting the USB Debugging
option to True.