Kotlin: How to get and set a text to TextView in Android using Kotlin?
just add below line and access direct xml object
import kotlinx.android.synthetic.main.activity_main.*
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
txt_HelloWorld.text = "abc"
}
replace activity_main according to your XML name
to set text in kotlin
textview.text = "write here"
In kotlin don't use getters and setters as like in java.The correct format of the kotlin is given below.
val textView: TextView = findViewById(R.id.android_text) as TextView
textView.setOnClickListener {
textView.text = getString(R.string.name)
}
To get the values from the Textview
we have to use this method
val str: String = textView.text.toString()
println("the value is $str")