Nitt
Nitt

Reputation: 1923

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

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.

How can one do this?

Upvotes: 58

Views: 218378

Answers (14)

darshan rajput
darshan rajput

Reputation: 1

For set text

textview.text = "xyz"

For get text

val string1 = textview.text

println(string1)

String1 value = xyz

Upvotes: 0

Mehmet Hikmet
Mehmet Hikmet

Reputation: 127

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"

Upvotes: 4

Md. Shahin Bashar
Md. Shahin Bashar

Reputation: 61

  • 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

Upvotes: 4

Ouahab Abdelwhab
Ouahab Abdelwhab

Reputation: 1

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.

Upvotes: -1

Arnold Brown
Arnold Brown

Reputation: 1433

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"

Upvotes: 3

Benny
Benny

Reputation: 2331

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

Upvotes: 6

Marium Jawed
Marium Jawed

Reputation: 419

to set text in kotlin

textview.text = "write here"

Upvotes: 8

sunghun
sunghun

Reputation: 1424

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

Upvotes: 0

AJ Snow
AJ Snow

Reputation: 93

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

Upvotes: -1

Venkatesh Jose
Venkatesh Jose

Reputation: 21

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

    }

}

Upvotes: 2

eagerprince
eagerprince

Reputation: 123

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

Upvotes: 0

tej shah
tej shah

Reputation: 3095

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

Upvotes: 13

Nithinlal
Nithinlal

Reputation: 5061

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

Upvotes: 90

Avijit Karmakar
Avijit Karmakar

Reputation: 9398

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

Upvotes: 6

Related Questions