I made a method to make sure my toast messages will be displayed immediately, without having to wait the previous toast to dissapear. The method :
public void myToaster(String message){
if(mToast!=null){
mToast.cancel();
}
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
I'm using Android Studio with API23.
mToast is never assigned/reassigned . This
mToast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
should be
mToast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
mToast.show();
Toast.cancel doesn't remove the current Toast immediately. The fade out animation takes place nevertheless
Related
I am trying to make a simple android app in which there is a toast message containing the number of the incoming call. I have stored the number in a string variable. When I display just the toast, it works fine but before the toast, if I add a simple if condition comparing the number to another string, the app quits. The required permissions are given. Can someone help me?
This Works:
public void onReceive(Context context, Intent intent) {
String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
}
This Does not Work (App quits)
public void onReceive(Context context, Intent intent) {
String incomingNumber = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(incomingNumber.equals("+919999999999"))
{
Toast.makeText(context, "Call from Mom", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
}
}
It sounds a little silly but I just wrapped my "if" condition in a try-catch block and it solved the problem. I am not used to programming in JAVA so it took me a lot to figure out this simple thing. Thank You All for providing me with suggestions :)
So my goal is to only have a toast message shown to the user if there is no toast message showing or if the message showing is NOT the same as the message I want to send. If the message IS the same as the one being shown to the user, I don't want the message to go through (because that is pointless).
To work towards this goal, I found this post on how to only show a toast if none are being shown.
I have modified the code to fit both requirements.
private Toast toast;
public void showAToast (String st, boolean isLong){
try{
toast.getView().isShown();
String text = ((TextView)((LinearLayout)toast.getView()).getChildAt(0)).getText().toString();
if(!text.equalsIgnoreCase(st)){
//New message, show it after
if(isLong){
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
} else {
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
}
toast.show();
}
} catch (Exception e) {
//New message
if(isLong){
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
} else {
toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
}
toast.show();
}
}
My issue is that any message will not go through if the last toast message was the same as the message that wants to go through.
Not sure exactly why this occurs, but I put some debugging messages in the method to figure out what the issue was.
The messages say that toast.getView().isShown() does not throw the exception (suppose to mean no toast is shown) if any toast message has been sent in the app's lifetime.
So my question is, how can I work around this? Surely there must be a way to achieve this desired functionality.
I saw this before in stackoverflow, but it's not nearly as clean as I would have liked. We implemented a dual toast approach, where it alternates between two toasts. First we define the toasts for the activity prior to the OnCreate:
Toast toast0;
Toast toast1;
private static boolean lastToast0 = true;
In the OnCreate:
toast0 = new Toast(getApplicationContext());
toast0.cancel();
toast1 = new Toast(getApplicationContext());
toast1.cancel();
//And finally, when I need to display the toast and cancel the prior toast at the same time I use something similar to:
if (lastToast0) {
toast0.cancel();
toast1.setDuration(Toast.LENGTH_LONG);
toast1.setText("new message");
toast1.show();
lastToast0 = false;
} else {
toast1.cancel();
toast0.setDuration(Toast.LENGTH_LONG);
toast0.setText("new message");
toast0.show();
lastToast0 = true;
}
// If you need to just cancel an existing toast (before it times out) use:
toast0.cancel();
toast1.cancel();
Quotation
You can use the same Toast instance to show message. If the message is same, the toast will no show twice time, otherwise the text will simple change to the latest one.
Toast mToast;
public void showToast(CharSequence message, int during){
if (mToast == null) {
mToast = Toast.makeText(getApplicationContext(), message, during);
} else {
mToast.setText(message);
}
mToast.show();
}
--↓---↓----↓---update--↓----↓---↓--↓
Sorry about that I miss your point above.
I read the source of Toast that we cannot get the view status since the view had been add to the WindownManager.So far, I cannot find out a method to point whether the Toast is shown.
But you can achieve your own Toast use Service,which likes a toast shown above the application. it may be easier.
The toast that says not reachable is not showing up, but all the above are showing up, why does my code stop execution on that line?
if (msg_from.equals(MainActivity.PHONENUMBER)){
if (msgBody.startsWith("Your")){
//have seen this toast
Toast.makeText(context, "Yes!!", Toast.LENGTH_SHORT).show();
//have seen this toast
LocationManager imLoca = SysService.locationMan;
Toast.makeText(context, "Yes 2!!", Toast.LENGTH_SHORT).show();
//have seen
boolean gpsEnabled = imLoca.isProviderEnabled(LocationManager.GPS_PROVIDER);
Toast.makeText(context, "not reachable", Toast.LENGTH_SHORT).show();
//but this does not show up, and any thing after this line is not working;
boolean netOn = imLoca.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gpsEnabled){
//do some thing
}
else if (netOn){
//do some thing
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
}
}
I copied this code into my test project and added a toast to the if and the else if for gpsEnabled and netOn.
It looks like the first if is the one that is getting executed as gpsEnabled is true.
Hope this helps, good luck.
if (gpsEnabled){
//do some thing
Toast.makeText(context, "1", Toast.LENGTH_SHORT).show();
}
else if (netOn){
//do some thing
Toast.makeText(context, "2", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
Imloca returns null.Problem is this.
I have an issue similar to Toast from FileObserver. However, I do not understand how to properly implement the Handler.
Currently, I have a FileObserver-class and I am passing it context and a handler (context comes from getApplicationContext() from the service I call the FileObserver-class from). The handler I pass in (handle) is defined and created in the service. In the onEvent() of the FileObserver-class, I have:
handle.post(new Runnable() {
public void run() {
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});
But, I end up with a java.lang.NullPointerException.
How do I properly make the toast notification show up when I send it from the onEvent() of the FileObserver-class?
I have an activity which gets called by 2 intents, one after a simple menu-selection and the other way by a intent after a deletion of an item in a database. However, I wanted to display in the called activity a little Toast, but only when it's opened through the intent of the deletion. I thought of following solution
public void intentCheck(){
Log.d("ShowActivity","intentCheck() called");
Bundle extras = getIntent().getExtras();
if (extras != null){
String check = extras.getString("AdvancedViewActivityCall");
if(check == "calling"){
Log.d("ShowActivity","delete-intent succeeded");
Toast success = new Toast(ShowActivity.this);
success.makeText(ShowActivity.this, "Deletion succeded", Toast.LENGTH_LONG);
}
}
but it doesn't work... somehow, no toast gets displayed.
edit:// i applied success.show(); now, but now i get a RunetimeException O.o ( http://pastebin.com/Th3NY5d0 )
edit: SOLUTION: Toast.makeText(context, text, duration).show(); //seems to be the "static way", which eclipse proposed
Have you tried if ("calling".equals(check)) instead of if(check == "calling") ?
EDIT:
try Toast.makeText(context, text, duration).show();
you have to call show method for toast until otherwise toast will not display.
success.show();