Android Toast Message is not showing

Android

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Solution 3 - Android

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

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

Solution 4 - Android

Just restart your device! It solved my problem.

Solution 5 - Android

Solution 6 - Android

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

Solution 7 - Android

I did like this

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

Solution 8 - Android

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

Solution 9 - Android

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

Solution 10 - Android

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

Solution 11 - Android

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.

Solution 12 - Android

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.

Solution 13 - Android

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

Solution 14 - Android

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

Solution 15 - Android

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!!

Solution 16 - Android

Try:

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

Solution 17 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPirabaView Question on Stackoverflow
Solution 1 - AndroidPocket UniverseView Answer on Stackoverflow
Solution 2 - Androiduser2288580View Answer on Stackoverflow
Solution 3 - AndroidLalit PoptaniView Answer on Stackoverflow
Solution 4 - AndroidAli MotameniView Answer on Stackoverflow
Solution 5 - AndroidPhilipp WendtView Answer on Stackoverflow
Solution 6 - AndroidHitesh SahuView Answer on Stackoverflow
Solution 7 - AndroidPirabaView Answer on Stackoverflow
Solution 8 - AndroidAtul O HolicView Answer on Stackoverflow
Solution 9 - AndroidAli NawazView Answer on Stackoverflow
Solution 10 - AndroidMina FaridView Answer on Stackoverflow
Solution 11 - AndroidEbin JoyView Answer on Stackoverflow
Solution 12 - AndroidMKJParekhView Answer on Stackoverflow
Solution 13 - AndroidXavier VegaView Answer on Stackoverflow
Solution 14 - AndroidMichaelView Answer on Stackoverflow
Solution 15 - Androidpushpo raniView Answer on Stackoverflow
Solution 16 - AndroidParesh MayaniView Answer on Stackoverflow
Solution 17 - AndroidArashView Answer on Stackoverflow