Piraba
Piraba

Reputation: 7004

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

Upvotes: 77

Views: 120728

Answers (19)

ysnsyhn
ysnsyhn

Reputation: 507

Try displaying on UIThread or MainThread

MainActivity.this.runOnUiThread(new Runnable() {
        public void run() {
    Toast.makeText(getBaseContext(), "show on uithread", Toast.LENGTH_SHORT).show();
        }
    });

Upvotes: 0

Ian Hern
Ian Hern

Reputation: 669

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

Upvotes: 0

Michael
Michael

Reputation: 117

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

Upvotes: 1

Hitesh Sahu
Hitesh Sahu

Reputation: 45160

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

Upvotes: 12

Xavier Vega
Xavier Vega

Reputation: 731

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

Upvotes: 1

Ebin Joy
Ebin Joy

Reputation: 3219

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.

Upvotes: 6

Mina Farid
Mina Farid

Reputation: 5887

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

Upvotes: 5

pushpo rani
pushpo rani

Reputation: 63

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

Upvotes: 0

Ali Motameni
Ali Motameni

Reputation: 2787

Just restart your device! It solved my problem.

Upvotes: 56

Ali Nawaz
Ali Nawaz

Reputation: 2510

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

Upvotes: 6

Atul O Holic
Atul O Holic

Reputation: 6792

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

Upvotes: 9

Arash
Arash

Reputation: 1

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.

Upvotes: -2

Lalit Poptani
Lalit Poptani

Reputation: 67296

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

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

Upvotes: 64

user2288580
user2288580

Reputation: 2258

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.

Upvotes: 77

Piraba
Piraba

Reputation: 7004

I did like this

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

Upvotes: 9

MKJParekh
MKJParekh

Reputation: 34301

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.

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128448

Try:

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

Upvotes: -1

Pocket Universe
Pocket Universe

Reputation: 1438

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.

Upvotes: 130

Related Questions