Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am currently in a android app development course and am brand new to kotlin. Right now I'm working on building a basic pizza ordering application that once the user makes selections and hits "submit" a textview is edited to show the total price... Except that I cant get the textview to display anything. What am i missing here because i followed the book step-by-step (but it seems to be fairly old) thank you so much!
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun onPlaceOrderButtonClicked(view: View) {
var pizzaSizePrice=0.0
var toppingsTotal = 0.0
when {
radioGroup.smallpizza.isChecked -> pizzaSizePrice=5.0
radioGroup.mediumpizza.isChecked -> pizzaSizePrice=7.0
radioGroup.largepizza.isChecked -> pizzaSizePrice=9.0
}
if (OnionsCheckBox.isChecked){toppingsTotal+=1}
if (OlivesCheckBox.isChecked){toppingsTotal+=2}
if (TomatoesCheckBox.isChecked){toppingsTotal+=3}
Totalprice.text=("Total order price= $" + (pizzaSizePrice+toppingsTotal))
}
}
It doesn't look like your code calls onPlaceOrderButtonClicked, try adding this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Calls your function when button is clicked
orderButton.setOnClickListener {
onPlaceOrderButtonClicked()
}
}
This will direct clicks from the button with id orderButton to onPlaceOrderButtonClicked(). Also, remove view: View from that function, it's not being used.
I'm guessing you aren't calling the onPlaceOrderButtonClicked(), there a number of ways to handle this. I'm my guess is your book is using the XML approach because the onPlacedORderButtonClicked() has a view argument. Try including an onClick in the button XML.
<Button
android:id="#+id/btn_orderpizza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLUS"
android:onClick="onPlaceOrderButtonClicked" />
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
Here's the code
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.myButton.setOnClickListener (object: View.OnClickListener {
override fun onClick(**v: View?**) {
binding.statusText.text = "Button Clicked"
}
})
}
}
Why does this code work when no argument is passed to onClick. I mean 'onClick()' takes a 'View' so we must call it like this: 'onClick(myButton)'.
The user interface control that is clicked, in this case a button, is a View, so it is passed as the argument to onClick().
Because the View.OnClickListener interface explicitly defines that the view that has been clicked is going to be passed to you.
You can certainly omit the value, but it's going to be there. Since it's a compiled language, in order to satisfy the interface you need to take that parameter regardless of whether you'll use it.
In JS world, which you may be getting confused with; is a dynamically typed language and hence in order for things to work you're just required to provide your callback. Either way, the parameters are still going to get passed into the function, but you'll not be referencing/using them
You typically never call onClick yourself. The framework does it for you when you physically touch the view. And it will pass the view you actually touched.
You might wonder:
Why does it even pass it?, I know which view I put the listener on
The thing is. You can give the same listener to multiple views. Having this as parameter makes you able to distinguish in the listener which of the views was clicked.
As a side note, this is a more idiomatic way to write it, and does exactly the same as your code
binding.myButton.setOnClickListener {
binding.statusText.text = "Button Clicked"
}
Writing it like this, will have the view parameter available as it
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Im a beginner in Android Studio and have a school project where I have to create an login screen with password and username. When trying to follow some instructions online I get an error even though I have done the same as the instructor. Can you see what I have done wrong?
Example image
Your code is in Kotlin while the video you linked uses Java, so the error indicates that the onClickListener is not following Kotlin syntax properly.
The equivalent in Kotlin is:
logIn.setOnClickListener {
// Do some work here
}
or
logIn.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View?) {
// Do some work here
}
})
Both will behave similarly. See alternative ways here.
// declare
private lateinit var logIn: Button
// cast
logIn = findViewById(R.id.logIn)
// execute the func you want
logIn.setOnClickListener {
executeLogInApi()
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
It seems that all logcat logs from within kotlin coroutines are swallowed. Is there a way to see logcat logs that are printed from within a coroutine?
It seems that all logcat logs from within kotlin coroutines are swallowed
Not normally. Normally, they work fine.
I just created a scrap Android Studio Arctic Fox project, using the Empty Activity template. I added implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1' to the dependencies, and I changed MainActivity to be:
package com.commonsware.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
GlobalScope.launch(Dispatchers.Default) {
Log.d("MyApplication", "um, hi!")
}
}
}
(GlobalScope is not a good choice in real work, but I wanted to minimize any hassle)
My Log.d() call worked just fine.
You might try reproducing this experiment and see what you get. Perhaps there is something tied to your environment, your project, or your particular use of coroutines that is causing your problems.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
when i run my app and click on button i need to see a message but Tost doesn't work..
what's the problem?
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.buttonSubmitUsername.setOnClickListener {
if (binding.textInputUsername.text.toString().isEmpty()){
Toast.makeText(this,"abc",Toast.LENGTH_SHORT).show()
}else{
val intent = Intent(this,QustionActivity::class.java)
startActivity(intent)
}
}
}
}
it was something wrong with my emulator...
if you have the same problem you should try another device.
It would be helpful if you provide the complete layout file for your MainActivity as well, but assuming from the naming convention textInputUsername, it seems you are trying to get text from your TextInputEdittext directly, you would have a TextInputLayout as well in your layout file. Try something like this:
binding.buttonSubmitUsername.setOnClickListener {
if (binding.textInputLayout.edittext?.text.toString().isEmpty()){ // `textInputLayout` is the id of your TextInputLayout
Toast.makeText(this,"abc",Toast.LENGTH_SHORT).show()
}else{
val intent = Intent(this,QustionActivity::class.java)
startActivity(intent)
}
}
}
}
But for the exact solution your layout file or some more details would be required, do tell if this fixed it :)
Try this:
Toast.makeText(this#MainActivity,"abc",Toast.LENGTH_SHORT).show()
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to disable the editText once timer is finished.
Have a look on the following code:
val CountDownTimer=object: CountDownTimer(10000,1000){
override fun onTick(millisUntilFinished: Long) {
txtTimer.setText(""+millisUntilFinished/1000)
}
override fun onFinish() {
Toast.makeText(this#Play_Area,"time up!",Toast.LENGTH_SHORT).show()
etResponse.isEnabled(false)
}
}
Here etResponse is EditText and when i make #isEnables(false) it is showing following error:
Too many arguments for public open fun isEnabled(): Boolean defined in android.widget.EditText
Please Help How to resolve it
You are using the wrong function.
editText.isEnabled() is used to return the enabled state only and it doesn't take an argument so you can call it like that
val state: Boolean = editText.isEnabled()
instead, you should use editText.isEnabled = true because the property is mutable
editText.isEnabled = false