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)
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}")
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}")
}
}
}
Related
why can't I access a variable from another class? (the variable is in the data class, and when I want to access it, it throws "unresolved reference")
Here's what It looks like:
The code that tries to access the variable:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class questionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_question)
var nameDisplay: TextView = findViewById(R.id.nameDisplay)
var name2 = usernameStore.username //here's the error, the "username" thing is red
nameDisplay.setText(name2)
}
}
The data Class:
package com.ketchup.myquizzies
public data class usernameStore (
var username: String
)
Any help appreciated guys, I literally searched all things that came to my mind to solve this, but I couldn't help myself :(
Android Studio, Kotlin
usernameStore is a class, not an object. You need to create an instance of usernameStore to be able to use it.
class questionActivity : AppCompatActivity() {
val store = usernameStore()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_question)
var nameDisplay: TextView = findViewById(R.id.nameDisplay)
var name2 = store.username
nameDisplay.setText(name2)
}
}
FWIW, creating instances of classes is covered by this section of this chapter of this free book.
You have not created instance of class
usernameStore
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class questionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_question)
var nameDisplay: TextView = findViewById(R.id.nameDisplay)
// replace with below code
var name2 = usernameStore().username
nameDisplay.setText(name2)
}
}
I don't know why text is not changing in the BirthdayGreetingActivity.
No error is coming but it's not changing text. I checked(by Logcat) that name has came successfully to BirthdayGreetingActivity.kt from MainActivity.kt.
I guess that this [binding.birthdayGreeting.text = "Happy Birthday $name"] line in BirthdayGreetingActivity.kt is not working properly.
MainActivity.kt --
package com.example.firstapp
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.example.firstapp.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
fun createBirthdayCard(view: View){
val name = binding.nameInput.editableText.toString()
// Toast.makeText(this,"Name is $name",Toast.LENGTH_SHORT).show()
val intent = Intent(this,BirthdayGreetingActivity::class.java)
intent.putExtra(BirthdayGreetingActivity.NAME_EXTRA,name)
startActivity(intent)
}
}
BirthdayGreetingActivity.kt --
package com.example.firstapp
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.firstapp.databinding.ActivityBirthdayGreetingBinding
class BirthdayGreetingActivity : AppCompatActivity() {
private lateinit var binding: ActivityBirthdayGreetingBinding
companion object{
const val NAME_EXTRA ="name_extra"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBirthdayGreetingBinding.inflate(layoutInflater)
setContentView(R.layout.activity_birthday_greeting)
val name = intent.getStringExtra(NAME_EXTRA)
// val greet = binding.birthdayGreeting.editableText.toString()
// print(name)
// if (name != null) {
// Log.d("greet = ",binding.birthdayGreeting.editableText.toString())
// }
binding.birthdayGreeting.text = "Happy Birthday $name" [maybe something wrong here]
}
}
:) :) Thanks for spending your valuable time to help me :) :)
On BirthdayGreetingActivity you are passing the layout ID for setContentView, instead you must pass the binding.root, just like you did on MainActivity...
Change this line:
setContentView(R.layout.activity_birthday_greeting)
To:
setContentView(binding.root)
On Clicking the btnBmi button, the app is crashing, & in the logcat the error showing for binding code.
I have added the code in the build.gradle file and synced it.
buildFeatures {
viewBinding true
}
// Code for .kt file.
package com.nandini.android.workoutapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.nandini.android.workoutapp.databinding.ActivityBmiCalculatorBinding
class BmiCalculatorActivity : AppCompatActivity() {
private var binding: ActivityBmiCalculatorBinding?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityBmiCalculatorBinding.inflate(layoutInflater)
setContentView(binding?.root)
setSupportActionBar(binding?.toolbarBmi)
if(supportActionBar!=null){
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.title="Calculate BMI"
}
binding?.toolbarBmi?.setNavigationOnClickListener {
onBackPressed()
}
}
}
The button to jump on this activity is in the activity_main.xml file with id : btnBmi , & intent code in the MainActivity.kt file.
// in the onCreate method of MainActivity.kt file.
binding?.btnBmi?.setOnClickListener{
val intent = Intent(this#MainActivity,BmiCalculatorActivity::class.java)
startActivity(intent)}
Step 1 change the Parent Theme in the style.xml/theme.xml-
parent="Theme.MaterialComponents.Light.NoActionBar"
Step 2 - Add toolbar in activity_bmi_calculator.xml
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:navigationIcon="#drawable/ic_home_black_24dp" />
Step 3
class BmiCalculatorActivity
: AppCompatActivity() {
lateinit var binding: ActivityBmiCalculatorBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBmiCalculatorBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.toolbar.title = "Calculate BMI"
binding.toolbar.setNavigationOnClickListener {
onBackPressed()
}
}
This question already has answers here:
Android - How to achieve setOnClickListener in Kotlin?
(32 answers)
Closed 1 year ago.
I am trying to get this toggle button to work - All I want to do is print in console "hello" but am getting this error
Type mismatch: inferred type is Unit but View.OnClickListener? was expected
package com.radiomedia.drn1
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
playButton.setOnClickListener(
print("hello")
)
}
}
Change setOnClickListener() method as any of the below options:
Option 1:
playButton.setOnClickListener{
print("hello")
}
Option 2:
playButton.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View?) {
print("hello")
}
})
Option 3:
playButton.setOnClickListener(View.OnClickListener { view ->
print("hello")
})
You need to use braces instead, like this:
playButton.setOnClickListener{
print("hello")
}
either
playButton.setOnClickListener {
print("hello")
}
or
playButton.setOnClickListener(object: View.OnClickListener {
override fun onClick(v: View?) {
TODO("Not yet implemented")
}
})
I add a textView in layout but when I'm trying to use Kotlin Android Extensions but I get Unsolved reference error on my TextView:
package com.normal.ff
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
fun first(a:Int, b:Int){
textView.text
}
Add import kotlinx.android.synthetic.main.activity_main.* at the top of your activity.
You are almost there, you just need a reference from layout file for that text view.
something like below
class MainActivity : AppCompatActivity() {
lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById<TextView>(R.id.textView)
}
}
fun first (a:Int, b:Int){
val text = textView.text
}