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()
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.
In Kotlin calling .text on your EditText is fine no need to do getText or toString
Toast.makeText(this, editTextHello.text, Toast.LENGTH_SHORT).show()
with button click
button?.setOnClickListener {
Toast.makeText(this,editText.text, Toast.LENGTH_LONG).show()
}
Even don't need findViewById
You're missing a cast of the View you get from findViewById to EditText. But you need a "if" if the control exist then get the text:
val editText = findViewById(R.id.editText_main) as EditText
if (editText != null) {
val showString = editText.text.toString()
Toast.makeText(this, showString, Toast.LENGTH_SHORT).show()
}