Home > OS >  Kotlin Android Studio, getText() not working as I want
Kotlin Android Studio, getText() not working as I want

Time:02-22

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

CodePudding user response:

You should try - catch for potential NumberFormatExceptions before converting a string to a float.

Plus, I think that you should move the values initialization in the click listener:

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 = try { 
        mEdit.getText().toString().toFloat() 
    } catch (e: NumberFormatException) { 0f }
    val liczbaa = liczba   1.5f

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

CodePudding user response:

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()
}
  • Related