How to get editText in kotlin and display with toast.
var editTextHello = findViewById(R.id.editTextHello)
I tried this but shows object
Toast.makeText(this,editTextHello.toString(),Toast.LENGTH_SHORT).show()
This is Kotlin, not java. You do not need to get the id of it. In kotlin, just write:
var editTextHello = editTextHello.text.toString()
use the beauty of kotlin ;-)
P.s: BTW, better to choose xml IDs like edx_hello and for the kotlin part, var editTextHello. Then you can differentiate between xml vars and kotlin vars.
You're missing a cast of the View
you get from findViewById
to EditText
:
var editTextHello = findViewById(R.id.editTextHello) as EditText
Then, you want to display the text
property of the EditText
in your toast:
Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show()
For the record, this is just the more idiomatic Kotlin equivalent to calling getText()
on your EditText
, like you'd do it in Java:
Toast.makeText(this, editTextHello.getText(), Toast.LENGTH_SHORT).show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With