toast message does not show up [closed] - android

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

Related

Kotlin: Is this a good way to avoid memory leaks? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 months ago.
Improve this question
I was always facing this memory leak warning without paying much attention to it, but now I'm looking into how to deal with it and, as I know I shouldn't use WeakReference and that sort of "tricks" to avoid it, come to what I think could be a possible and simple solution.
My idea is as follows:
I have a singleton class (object) which holds all my app configuration, where I initialize a context from the Application class like this:
object AppSettings {
lateinit var context: Context
fun init(appContext: Context){
this.context = appContext
}
/* OTHER STUFF */
}
typealias aw = AppSettings
#HiltAndroidApp
class AWApplication : MultiDexApplication() {
override fun onCreate() {
super.onCreate()
AppSettings.init(this)
}
/* OTHER STUFF */
}
I initialize that context not only in ApplicationClass, but in every activity OnCreate (which inherits from BaseActivity):
#AndroidEntryPoint
open class BaseActivity {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AppSettings.init(this)
}
}
And finally, I can access context wherever it is needed as follows:
object RandomObject {
fun DoWhatever() {
PlayAFreddieMercurySong(aw.context,)
}
}
Well, this is my possible solution and I would like to know Android gurus from SO opinion about it.
Maybe I'm leaking memory in my App Settings -where I had initially store context-, but Android Studio is not complaining about it, so I am not sure.
In the end, I'm trying to avoid passing context as a method parameter in every place it is needed for code simplicity.
"other stuff" are common between all activities and they need just ApplicationContext, then why you don't use application context in AppSettings. and thats it. BTW your solution will not leak, if and only if you call your AppSettings.init(this) in all activities.
and you don't guarantee that .
in other words "the code doesn't leak now but may be in the future" - vulnerable
if you have functions thats related to activities, fragments or any
class you want, you can use extension functions for that
you should create a kotlin file with name for ex ActivityExt. and write all of your cases that you need for activities . if you need functions for fragments you should also create another kotlin file with name FragmentExt..etc

Why don't we need to pass an argument to onClick() in android [closed]

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

I get an error (unresolved reference) in Android Studio when trying to use "new View.OnClickListener" [closed]

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

New to kotlin - textview editing is failing [closed]

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

Too many arguments for public open fun isEnabled() [closed]

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

Categories

Resources