Android Toast Message is not showing - android

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

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.

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.

I think you are missing .show();
It should be...
Toast.makeText(getBaseContext(), "Amount can not be grater than invoice",
Toast.LENGTH_SHORT).show();

Just restart your device! It solved my problem.

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

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

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

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

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

Simply restart your emulator not fixed my problem.
Close emulator
Tools -> Avd Manager
In the device list click on "drop-down icon" under "Action" column.
Cold boot now
Now reinstalling the app it will work.

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.

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.

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

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

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

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.

Related

Android Toast prevents my devices to sleep

I use Toast to indicate my app's state. And I use the following code in order to control toast showing time.
Toast noCardDetectedToast=null;
void setVisibilityNoCardDetectedToast(boolean visible)
{
if(visible)
{
if(noCardDetectedToast==null)
noCardDetectedToast = Toast.makeText(this, R.string.msg_no_card_detected, Toast.LENGTH_SHORT);
noCardDetectedToast.show();
}
else
{
if(noCardDetectedToast!=null)
{
noCardDetectedToast.cancel();
noCardDetectedToast=null;
}
}
}
When it's necessary to show this Toast, the application starts invoking setVisibilityNoCardDetectedToast(true) several times per second.
And when the application doesn't need this toast any more, it invokes setVisibilityNoCardDetectedToast(false).
Everything works fine, but my android device does not fall asleep, as long as the toast is visible.(I tested my application on Android 4.x and 5.0)
This behaviour looks strange for me. What do I do wrong here?

Toast Alert Dialog is not displaying in ListView

I wrote an Application in which we can add items to the list view..i'm doing validation for that if any one making the same entry it should display an alert
Toast.makeText(getApplicationContext(), "Serial No Already Exist..",Toast.LENGTH_LONG);
But it not displaying while I'm running on emulator. I'm sure that my validation code is correct.
Try:
Toast.makeText(getApplicationContext(), "Serial No Already Exist..",Toast.LENGTH_LONG).show();
add the show() to the toast like this
Toast.makeText(getApplicationContext(), "Serial No Already Exist..",Toast.LENGTH_LONG).show();
You need to specify show() to make the toast visible
Everything is perfect except one that you forget to append .Show() at the end, and also check this .
Toast.makeText(getApplicationContext(), "Serial No Already Exist..",Toast.LENGTH_LONG).Show();
Instead#
Toast.makeText(getApplicationContext(), "Serial No Already Exist..",Toast.LENGTH_LONG);

Why does logcat ignore log statements with tag "PHONE"?

I was setting a listener on my call button and at first I just wanted to make sure that the listener was working, so I put a log statement inside of it. But for some mysterious reason, it refused to print when I clicked it! So maybe the call button was null, I thought, and added an else statement...but it didn't print anything from either the if or the else statement!!! It would print the statements before and after, but totally ignore everything in the if-else structure. Here's the code:
ImageButton call = (ImageButton) v.findViewById(R.id.callButton);
Log.d("MEETINGS", "ABOUT TO WORK W/ CALL");
Log.e("MEETINGS", "" + (call != null));
if (call != null) {
Log.d("PHONE", "setting stuff on call...");
call.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("PHONE", "on call : " + phone);
}
});
} else {
System.out.println("why is this messed up");
Log.d("PHONE", "call button was null!");
}
System.out.println("what the heck is going on");
After at least 15 minutes of trying to unravel the mystery of how Java could just decide to skip both the if and the else, I tried giving the log statement a different tag.
And eureka! That did the trick! I changed "PHONE" to "BLAH" and suddenly the world made sense again! Curious, I changed it to "phone" and it refuses to print again.
Moral of the story: never use the tag "phone" or "PHONE" in logcat!!!
Can someone please explain to me how and/or why logcat ignores messages with the tag "PHONE"?
See the documentation for isLoggable(). I'm guessing debug output for the "PHONE" tag has been disabled on your phone to suppress output from the actual phone application.
I believe you should be able to run "getprop log.tag.PHONE" in a shell on the phone to retrieve the minimum severity level required for messages tagged "PHONE" to be printed.

Android Toast Won't Disappear

I show the toast, it doesn't disappear, even after the app is closed. How do I fix?
#Override
public void onClipStoreLoadedClipsNotification(ClipStoreLoadedClipsNotification notif)
{
final ClipStoreLoadedClipsNotification notification = notif;
runOnUiThread(new Runnable() {
#Override
public void run()
{
Dialogs.DismissAll();
list.onRefreshComplete();
TextView text = (TextView)findViewById(R.id.loadclipstext);
ProgressBar pb = (ProgressBar)findViewById(R.id.loadclipsprogress);
if (notification.moreClipsAvailable)
{
text.setText(context.getString(R.string.loading_clips));
pb.setVisibility(View.VISIBLE);
}
else
{
text.setText(context.getString(R.string.no_clips));
pb.setVisibility(View.INVISIBLE);
int duration = Toast.LENGTH_SHORT;
Toast.makeText(SugarLoafContext.playbackTabContext, "No clips found.", duration).show();
}
SugarLoafContext.currentCamera = notification.camera;
clipList = notification.clips;
refreshListView();
readyToLoadMoreClips = true;
if (!firstClipsLoaded)
firstClipsLoaded = true;
}
});
}
Is it running inside a IntentService???
If it is so, the problem is that the Intent Services in Android run in a different thread than the main one, so the Toast is shown in a different thread than the main one, after the time to show is finished, the Android system is unable to find where is the Toast, so it can´t remove it.
I had the same problem, and now, i recommend everyone NOT to show Toast inside a IntentService, instead try to run one commom Service, or to open an Activity, or try something different if it is completely necessary to show the Toast.
The fact that the Toast doesn´t dissapear when you close the App is that the IntentService is still running, and you have to reboot the system or to uninstall the App for the Intent Service to be close.
The only explanation is that your Toast is called in a loop. You should track the toast .show() and see if it is not called an infinite times.
An other visual way would be to do this
Toast.makeText(SugarLoafContext.playbackTabContext, "No clips found.", duration).show();
Toast.makeText(SugarLoafContext.playbackTabContext, "Do you understand now?", duration).show();
I am sure you will see both toast alternatively during a looong time...
This can also happen if your device has no network connection, and your app does a license check and shows a Toast of results. One of my apps had this problem, as I was displaying a Toast when checking the app was licensed. Without a network connection the Toast telling the user that a license retry was to be done remained, and when I connected the Toast was removed, because the license check could work.

Display Error messages on Screen(view)

I want to show Error(log) messages from catch block. How can I show all messages(stack) on a single Screen, so that user can get an idea ?
Thanks...
How about using Toast?
Sample:
Toast.makeText(getBaseContext(), getResources().getString(resourceIDForMessage), Toast.LENGTH_SHORT).show();

Categories

Resources