78

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

Gary
  • 13,303
  • 18
  • 49
  • 71
Piraba
  • 6,974
  • 17
  • 85
  • 135
  • 3
    As pointed by one comment below, you also should enable 'Show notification' for the application in order Toast to work for you. – Atul Apr 09 '16 at 07:32
  • 1
    Why did you edit the code to include the solution? It should be preserved to contain the error, otherwise this question does not make sense. – Fato39 May 11 '21 at 12:35
  • 1
    Also a common gotcha: if you are not in the main thread you need to call `runOnUiThread`. E.g. `runOnUiThread(() -> Toast.makeText(getApplicationContext(), " ...", Toast.LENGTH_LONG).show());` – Atan Sep 26 '21 at 14:14

18 Answers18

129

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.

Pocket Universe
  • 1,428
  • 1
  • 9
  • 9
  • 11
    Oh god why. Been a couple hours stuck because of that! – CJ_COIMBRA Jan 31 '17 at 14:30
  • 5
    This. Is. Silly. – Gibolt Oct 09 '17 at 08:37
  • I have a feeling I am going to make this error quite a few times. If you do jQuery it's kinda similar to their method to `.show()` and `.hide()` DOM elements, so if you remember that the Toast is always hidden by default that might help remember to do the `.show()` – Jim Factor Jun 08 '18 at 22:46
  • I have a .show() and I am calling right from the activity's onCreate method and I also have use `this`, `MainActivity.this`, `getApplication()`, `getApplicationContext()`, `getBaseContext()` all to no avail. The toast showed on the first two test and never showed again. No code changes between that period. –  Jan 26 '19 at 08:46
  • @RichardMcFriendOluwamuyiwa Yet there's always that little detail you missed, I know from first hand experience.. – Mihael Keehl Mar 10 '19 at 15:41
77

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.

user2288580
  • 2,210
  • 23
  • 16
  • 5
    I would have never thought of that... Thought this setting only hides the notification icon of my service, but you're right, it did hide toasts as well – prom85 Jul 19 '15 at 19:11
  • 3
    Whoa, I was extremely skeptical of this, but at least on 4.4.4, disabling notifications also stops toasts from displaying. – Taylor Kline Aug 04 '15 at 14:25
  • I did this too and wasted 30 minutes of my time :( – StoneLam Nov 15 '17 at 05:06
64

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

Toast.makeText(getBaseContext(), "Amount can not be grater than invoice",
                                                     Toast.LENGTH_SHORT).show();
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
56

Just restart your device! It solved my problem.

Atef Hares
  • 4,715
  • 3
  • 29
  • 61
Ali Motameni
  • 2,567
  • 3
  • 24
  • 34
  • 2
    OMG yes, for anyone reading this answer, he means "restart your device". I almost downvoted your answer... xP – varun Mar 08 '19 at 20:15
  • 2
    Dude, you just saved me a whole day! I was showing a toast like `Toast.makeText(context, message, Toast.LENGTH_SHORT).show();` and it wasn't appearing suddenly. Got me questioning my abilities and meaning of life... Device restart fixed it! – Georgi Nov 12 '19 at 11:23
  • Wow. Works in 2020. Pixel 2 Device Build QQ1A.191205.008). Excellent! – Treewallie Jan 18 '20 at 18:20
  • This worked for me on my emulated AVD Pixel 3a. I had tried everything else. Thanks! – totaltotals Nov 18 '20 at 20:48
  • 1
    Worked for me on MiA2. Good job I found this. I was just about to start modifying code that had been working! – Sparers Feb 05 '21 at 21:28
  • It works, thanks. Anyone knows why and how? – lubrum Jun 11 '21 at 15:07
  • If someone told me I would not believe, but it actually was the case for me on the Google Nexus 5x (Android 8.1). – Vit Khudenko Jun 14 '21 at 23:13
  • Dude, you are very nice person! I spent about an hour to solve that, It was actually that simple! I've almost started to read Android sources to understand the root of this problem:) – Akhmedzianov Danilian Jun 23 '21 at 20:42
14

Maybe you are not in the UI thread? Try this: http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29

Philipp Wendt
  • 2,538
  • 1
  • 15
  • 17
10

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()
        }
    }
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • Calling the empty Handler constructor off the main thread will create a Handler that doesn't post to the main thread. You need to specify the main Looper. – Tenfour04 Oct 08 '21 at 13:19
9

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

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
9

I did like this

Toast.makeText(SalesActivityGroup.group.getParent(), "Amount can not be 
                                grater than invoice", Toast.LENGTH_SHORT).show();
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Piraba
  • 6,974
  • 17
  • 85
  • 135
6

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

Ali Nawaz
  • 2,016
  • 20
  • 30
6

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.

Ebin Joy
  • 2,690
  • 5
  • 26
  • 39
5

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

Mina Farid
  • 5,041
  • 4
  • 39
  • 46
1

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()
    }
Xavier Vega
  • 701
  • 5
  • 6
1

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

Michael
  • 183
  • 2
  • 10
1

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.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
0

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

0

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();
Ian Hern
  • 641
  • 1
  • 8
  • 16
-1

Try:

Toast.makeText(v.getContext(), "Reason can not be blank", Toast.LENGTH_SHORT).show();
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
-2

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.

Arash
  • 1
  • 1