I'm newbie.
I want to change text of
TextView
While click on it.
My code:
val text: TextView = findViewById(R.id.android_text) as TextView
text.setOnClickListener {
text.setText(getString(R.string.name))
}
Output: I got the output but showing use property access syntax.
Can anyone tell me how to do it?
Thanks in advance.
Find the text view from the layout.
val textView : TextView = findViewById(R.id.android_text) as TextView
Setting onClickListener on the textview.
textview.setOnClickListener(object: View.OnClickListener {
override fun onClick(view: View): Unit {
// Code here.
textView.text = getString(R.string.name)
}
})
Argument parentheses can be omitted from View.setOnClickListener if we pass a single function literal argument. So, the simplified code will be:
textview.setOnClickListener {
// Code here.
textView.text = getString(R.string.name)
}
to set text in kotlin
textview.text = "write here"
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
import kotlinx.android.synthetic.main.MainActivity.*
class Mainactivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.MainActivity)
txt.setText("hello Kotlin")
}
}
Use textView.text
for getter and setter, ex:
val textView = findViewById<TextView>(R.id.textView)
// Set text
textView.text = "Hello World!"
// Get text
val textViewString = textView.text.toString()
<TextView
android:id="@+id/usage"
android:layout_marginTop="220dip"
android:layout_marginLeft="45dip"
android:layout_marginRight="15dip"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google "
android:textColor="#030900"/>
usage.text="hello world"
findViewById(R.id.android_text) does not need typecasting.
The top post has 'as TextView' appended on the end. You might get other compiler errors if you leave this on. The following should be fine.
val text: TextView = findViewById(R.id.android_text) as TextView
Where 'android_text' is the ID of your textView
Yes its late - but may help someone on reference
xml with EditText, Button and TextView
onClick on Button will update the value from EditText to TextView
<EditText
android:id="@+id/et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_submit_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txt_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Look at the code do the action in your class
Don't need to initialize the id's of components like in Java. You can do it by their xml Id's
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sample)
btn_submit_id.setOnClickListener {
txt_id.setText(et_id.text);
}
}
also you can set value in TextView like,
textview.text = "your value"
Source: Stackoverflow.com