Kotlin: How to get and set a text to TextView in Android using Kotlin?

Viewed 171978

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.

13 Answers

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"

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"
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")

    }

}
  • go to build.gradle(:app) file

add dependencies{

 apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
  • after that go to your Activity file and write this line

import kotlinx.android.synthetic.main.activity_main.*

Done!!

now you may direct find your widget using their id.

Thanks

findViewById(R.id.android_text) does not need typecasting.

Kotlin synthetics have deprecated therefore you can use bindingView method. after implementing basefragment or baseActivity you can use binding as in below:

binding.textView.text ="type your text here"

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

This procedure works for me:

Go to build.gradle(Module: -> after dependencies { add these lines:

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

You can watch the video in this link.

Related