Android Toast Message is not showing

I have Button.When the user click the button, there are some condition, that condition is not satisfy then need to display Toast but not showing Toast Message...

Code: Edited

 Button addMe = (Button)findViewById(R.id.addMe);
addMe.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(selectedReason.equals("--Select--")){
Log.i("TAG","-----");
Toast.makeText(getBaseContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();
}else if(selectedType.equals("--Select--")){
Toast.makeText(getParent(), "Discount type can not be blank", Toast.LENGTH_SHORT).show();
}else{
if(selectedType.equals("Value")){
if(spc_amount.getText().toString().equals("")){
Log.i("TAG","-----");
Toast.makeText(getBaseContext(), "Discount type can not be blank", Toast.LENGTH_SHORT).show();
}else{
if(Double.parseDouble(spc_amount.getText().toString()) > invoiceValue){
Toast.makeText(getBaseContext(), "Amonut can not be grater than invoice", Toast.LENGTH_SHORT).show();
}else{
Discount dis = new Discount();
dis.setCriteriaName(selectedReason);
dis.setDiscountValue(Double.parseDouble(spc_amount.getText().toString()));
spDisList.put(1,dis);
tl.removeAllViews();
loadTableLayout();
}


}
}
}
}
});

I have tried context with getParent() , getApplicationContext() , SpecialDiscountActivity.this & getBaseContext() but not working....

This Toast message coming under the Tab Activity Group

106434 次浏览

Try:

Toast.makeText(getBaseContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();

It's the .show() that you've omitted everywhere that causes all your toasts to instatiate, but never execute.

I think you are missing .show(); It should be...

Toast.makeText(getBaseContext(), "Amount can not be grater than invoice",
Toast.LENGTH_SHORT).show();

Try:

Toast.makeText(v.getContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();

You can use the context of the Activity

like,

Toast.makeText(ActivityName.this,"Reason can not be blank", Toast.LENGTH_SHORT).show()

if this is not working, please put a log.i(); in your each condition may be its going to the last else and you are not getting the Toast.

I did like this

Toast.makeText(SalesActivityGroup.group.getParent(), "Amount can not be
grater than invoice", Toast.LENGTH_SHORT).show();

Please excuse me if this isn't the solution to your problem, but I accidentally unchecked the 'Show notification' setting of the application once. It may be an idea go into your device settings/application/manager and check this setting.

Complimer checks all the code and if there is a critical error, it ignores even the lines before the error section and therfore you will not see the "Toast". Simply, comment the lines which error happens in them (you can find the error lines in Logcat) Now you can see the "Toast" and can analyze your problem.

Just bumped across this issue and was wondering how a simple Toast is not appearing. After few trials it struck me to check the notification setting of the app and Voila.

I switched the notification setting ON and it started showing up. I searched and came across the below link, which talks about the same:

https://code.google.com/p/android/issues/detail?id=35013

There could be two possibilities:

1 - You have not append .show(); at the very end of Toast.makeText(getActivity(), "Hi", Toast.LENGTH_SHORT).

2 - There could be situation you are not passing right context

For that try passing getActivity().getApplicationContext();

which in my case resolved the issue.

Good luck :)

Just restart your device! It solved my problem.

If Toast is not showing that means either you have not called show() method or you are not on UI thread

Ideally, you should create a helper method to show Toast like this on UI thread

 /** Execute Toast on UI thread **/
private fun showToast(message: String) {


Handler(Looper.getMainLooper()).post {
// Code here will run in UI thread
Toast.makeText(
this,
message,
Toast.LENGTH_LONG
).show()
}
}

Sometimes there may be an error or nullPointerException in the message that we want to print with Toast message. If this error occured then app will simply ignore the Toast message. I had the same issue. I printed the message in Log and found the error, after that I changed the message in Toast. And it worked as I wanted. Try it!!

Some times Emulator is hanging so restarting the emulator fixes the issue.

Simply restart your emulator not fixed my problem.

  1. Close emulator
  2. Tools -> Avd Manager
  3. In the device list click on "drop-down icon" under "Action" column.
  4. Cold boot now

Now reinstalling the app it will work.

In my case it was because I wasn't running from Main thread, this fixed it:

    val mainActivity = (tabHostActivity.activityContext() as MainActivity)
mainActivity.lifecycleScope.launch{ //Dispatchers.Main, follows lifecycle
Toast.makeText(mainActivity, "my awesome message", Toast.LENGTH_LONG).show()
}

Just Cold boot your device! It can solve the problem.

Android throttles toats so if you send to many they just stop showing up, instead you can use Snackbar:

Snackbar.make(myView, "This is a snack.", Snackbar.LENGTH_SHORT).show();