56

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?

starball
  • 20,030
  • 7
  • 43
  • 238
Nitt
  • 1,813
  • 3
  • 12
  • 15

13 Answers13

87

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")
Nithinlal
  • 4,845
  • 1
  • 29
  • 40
  • 2
    As an additional hint, you can press Alt+Enter in Android Studio when on a warning to get quick actions for fixing it. – zsmb13 May 21 '17 at 13:16
  • 1
    The types will be inferred also so `val text: TextView` and `val str: String` are not absolutely necessary. – Daniel Storm May 21 '17 at 14:46
  • 1
    I am very new to Android, but I had to use resources.getString(R.string.name) – Scooter Apr 21 '18 at 19:11
  • Is it possible to set text withot ID? [My Question link](https://stackoverflow.com/q/64714568/5995648) – Arbaz.in Nov 06 '20 at 12:29
12

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

tej shah
  • 2,995
  • 2
  • 25
  • 35
8

to set text in kotlin

textview.text = "write here"
Marium Jawed
  • 391
  • 4
  • 9
6

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)
}
Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
6

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()
Benny
  • 2,233
  • 1
  • 22
  • 27
4
  • 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

  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29778662) – gtxtreme Sep 09 '21 at 08:18
4

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"

3

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"
Arnold Brown
  • 1,330
  • 13
  • 28
2
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")

    }

}
user70960
  • 326
  • 1
  • 16
0
  <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"
eagerprince
  • 115
  • 1
  • 5
0

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

sunghun
  • 1,424
  • 4
  • 25
  • 49
-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.

Vadik Sirekanyan
  • 3,332
  • 1
  • 22
  • 29
-2

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

BiRjU
  • 733
  • 6
  • 23
AJ Snow
  • 1
  • 1