-1

I just started learning kotlin, and in my app I want to take value placed by user from EditText to my MainActivity and work on it (for example add 1.5 to that value and show that value on the screen), but I have no idea how to do it, the code I wrote so far:

val btnClick= findViewById<Button>(R.id.btn)
        val tekstNaButtonie = findViewById<TextView>(R.id.siema)
        val mEdit = findViewById<EditText>(R.id.editTextTextPersonName)
        val strrr = mEdit.getText()
        val strrrr = strrr.toString()
        val liczba = strrrr.toFloat()
        val liczbaa = liczba + 1.5f

        btnClick.setOnClickListener {
            tekstNaButtonie.text = liczbaa.toString()
            Toast.makeText(this@MainActivity,"Kliknales mnie!",Toast.LENGTH_LONG).show()
        }

My Xml:

android:id="@+id/editTextTextPersonName"
        android:layout_width="100sp"
        android:layout_height="48sp"
        android:layout_marginStart="16sp"
        android:layout_marginTop="12sp"
        android:ems="10"
        android:hint="0"
        android:importantForAutofill="no"
        android:inputType="numberDecimal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

with this code app don't want to start: enter image description here

  • When Im deleting this lines of code: ```val strrrr = strrr.toString() val liczba = strrrr.toFloat() val liczbaa = liczba + 1.5f``` and just do ```strrr.toString()``` the app works perfectly fine – Jan Kowalski Feb 21 '22 at 11:10
  • Your app is crashing, check stack trace for crash logs. – Heshan Sandeepa Feb 21 '22 at 11:12
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Ivo Feb 21 '22 at 11:14
  • From crash logs it seemed that there was an empty string, so I added: ```android:text="0"``` to my EditText in xml and it works, howefer I am hoping to find some better solution, cause I don't want to delete 0 every time I want to type some number in the app – Jan Kowalski Feb 21 '22 at 11:21
  • Plus ```android:text``` is always 0 now, and I want it to be 0 only when user won't put anything in there – Jan Kowalski Feb 21 '22 at 11:28

1 Answers1

0

I think it would be easiest to do this using toFloatOrNull(). It will return null instead of throwing an exception if the text is not a number. Then you can skip doing the calculation if the number is null. Optionally you can show a message to the user.

Also, you need to be checking these values inside your button, or the button will only see what the original value was.

val btnClick= findViewById<Button>(R.id.btn)
val tekstNaButtonie = findViewById<TextView>(R.id.siema)
val mEdit = findViewById<EditText>(R.id.editTextTextPersonName)

btnClick.setOnClickListener {
    val liczba = mEdit.getText().toString().toFloatOrNull()
    if (liczba != null) {
        val liczbaa = liczba + 1.5f
        tekstNaButtonie.text = liczbaa.toString()
        Toast.makeText(this@MainActivity,"Kliknales mnie!",Toast.LENGTH_LONG).show()
    } else {
        //Maybe show user an error message
    }
}

Or if you want to treat invalid input as 0, you can use ?: the elvis operator to provide a default of 0:

val btnClick= findViewById<Button>(R.id.btn)
val tekstNaButtonie = findViewById<TextView>(R.id.siema)
val mEdit = findViewById<EditText>(R.id.editTextTextPersonName)

btnClick.setOnClickListener {
    val liczba = mEdit.getText().toString().toFloatOrNull() ?: 0f
    val liczbaa = liczba + 1.5f
    tekstNaButtonie.text = liczbaa.toString()
    Toast.makeText(this@MainActivity,"Kliknales mnie!",Toast.LENGTH_LONG).show()
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • Using ```mEdit.getText().toString().toFloatOrNull() ?: 0f``` and placing values in click listener completely solved my problem. Thank you! – Jan Kowalski Feb 21 '22 at 18:27