1

I was following some guide(download android studio today) of kotlin and I have use the setText and it's not working. what will be the problem?

package com.example.basic

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener {
            Toast.makeText(applicationContext, "button was pressed.", Toast.LENGTH_LONG).show()
        }

        button2.setOnClickListener {
            val input = editTextTextPersonName.text.toString()
            TextView.setText("entered value: ${input}")
        }
    }
}

(I had tried replace setText to text but it's still red and can't save it)

Unresolved reference: setText(error)

2
  • Attach the error log please Commented Feb 4, 2022 at 10:39
  • 2
    It seems you're using the TextView class itself but I assume you want to use your view. maybe it starts with a small case textView. Check the id of that TextView in XML. You may want to change the id, if it has the same name as the class. Commented Feb 4, 2022 at 10:42

2 Answers 2

4

TextView is the name of the class. You need to apply setText on an instance of the class. just like you did

editTextTextPersonName.text.toString()

instead of

EditText.text.toString()

I don't know that your TextView is called but you then need to do

instanceOfYourTextView.setText("entered value: ${input}")
Sign up to request clarification or add additional context in comments.

2 Comments

Thank! But even after following what you said, setText didn't work.
Oh! I think I was missing the TextView's id. It was 'TextView2'. Thanks!
0

As Mayur Gajra did mention you are not using the view from the XML but instead you are using the TextView class and this is your problem, what you need to have instead is something like this:

<TextView
     android:id="@+id/text"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text=""
     />

And then your MainActivity should look like the following:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener {
            Toast.makeText(applicationContext, "button was pressed.", Toast.LENGTH_LONG).show()
        }

        button2.setOnClickListener {
            val input = editTextTextPersonName.text.toString()
            text.setText("entered value: ${input}")
        }
    }
}

2 Comments

Oh! I think I was missing the TextView's id. It was 'TextView2'. Thanks!
Sure no problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.