Home > Blockchain >  how to set a number after calculating on a text view received by an edit text
how to set a number after calculating on a text view received by an edit text

Time:11-15

How can i put the number after calculated on the text view?? i can share more files if needed


    fun calcularIdade() {
        val editTextHello = findViewById<TextInputEditText>(R.id.Date)
        var num= Integer.parseInt(editTextHello.toString())
        num = 2022-num
        Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show()
        findViewById<TextView>(R.id.textView1).setText(num.toString())
        }

    }
    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@ id/Date"
        android:hint="“Escreva o seu ano de nascimento!”"
        android:inputType="number"/>
  </com.google.android.material.textfield.TextInputLayout>


  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/textDT"
      android:layout_marginStart="10dp"
      android:layout_marginTop="10dp"
      android:layout_marginEnd="10dp"
      android:layout_marginBottom="10dp"
      android:orientation="horizontal">


    <TextView
        android:id="@ id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_weight="1"
        android:text="TextView" />

    <Button
        android:id="@ id/B1"
        android:onClick="calcularIdade"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_weight="1"
        android:text="Calcular" />
  </LinearLayout>



</RelativeLayout>

activity_data_nascimento.xml mainactivity

Android Studio

Need to set a number on the text view after calculating I have tried everything but i cant my main problem is change the value from string to integer calculate and then write the value on the text view

CodePudding user response:

Use var num= Integer.parseInt(editTextHello.text.toString())

instead var num= Integer.parseInt(editTextHello.toString())

You have to parse the value inside TextInputLayout

CodePudding user response:

Shorter version

val num = editTextHello.text.toString().toInt()
  • Related