Reputation: 1149
I am trying to show a toast message in my application using the following code.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Do you want to continue?");
alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try{
//This code generates an Activity Not Found exception
}
catch(ActivityNotFoundException e) {
System.out.println("Activity Not Found Exception Raised");
Toast.makeText(getBaseContext(), "Activity Not Found", Toast.LENGTH_LONG).show(); // For the context I tried using getBaseContext, ActivityName.this also
}
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
But this message is being shown only on few devices. I've tested this code on HTC One X having Android version 4.2.2 which is working.
The same code if I test on Micromax A63 which is also having Android 4.2.2, but it doesn't work on it.
I've searched over Internet for this kind of errors and they are mostly telling about the app notification disabling option in the settings menu. But my application notification are not disabled.
EDIT
I'm doing it inside an AlertDialog
Can someone help me solve this issue.
Upvotes: 3
Views: 9136
Reputation: 863
In case you haven't figured this out yet, make sure you haven't disabled notifications for the application in question; this also disables toasts.
https://code.google.com/p/android/issues/detail?id=35013
Upvotes: 15
Reputation: 75788
Try this
Toast.makeText(getBaseContext(), "My Toast Message", Toast.LENGTH_SHORT).show();
OR
Toast.makeText(PreferenceActivity.this, "My Toast Message", Toast.LENGTH_SHORT).show(); `
For more details .Check THIS
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Do you want to continue?");
alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try{
//This code generates an Activity Not Found exception
}
catch(ActivityNotFoundException e) {
System.out.println("Activity Not Found Exception Raised");
ShowToast();
}
}
});
alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
public void ShowToast()
{
Toast.makeText(getBaseContext(), "Activity Not Found", Toast.LENGTH_LONG).show(); // For the context I tried using getBaseContext, ActivityName.this also
}
Upvotes: 2
Reputation: 2499
If you are using it in an activity then use:
Toast.makeText(ActivityName.this, "My Toast Message", Toast.LENGTH_SHORT).show();
And if You are using it for fragments then:
Toast.makeText(getActivity, "My Toast Message", Toast.LENGTH_SHORT).show();
OR in Adapter
Toast.makeText(context, "My Toast Message", Toast.LENGTH_SHORT).show();
Note: here in adapter the context means the context you declared in your adapter.
Upvotes: 4
Reputation: 640
Context context;
1.Then call and initialize on OnCreate()
context=this; (Use in Activity)
context=this.getActivity(); (Use in Fragment)
Then use
Toast.makeText(context, "My Toast Message", Toast.LENGTH_LONG).show();
Upvotes: 0