Android - Hide All Shown Toast Messages

how to hide Toast messag from showing up

What's happening is that you have three places in your activity where you call this:

InternetStatus.getInstance(holder.blogLikeBtn.getContext()).isOnline()

If you do not have internet, you will call this line of code three times, and the three toasts will be created that appear sequentially; one after another:

Toast.makeText(context, "Check Internet", Toast.LENGTH_SHORT).show();

You should try and consolidate your code so that the toast is only shown one time. Consider the following code:

    if (InternetStatus.getInstance(holder.blogLikeBtn.getContext()).isOnline()) {
//Get Likes
firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).addSnapshotListener(((Main2Activity) context), new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {

if (e != null) {
Log.w(TAG, "listening failed", e);
return;
}

if (documentSnapshot.exists()) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.blogLikeBtn.setImageDrawable(context.getDrawable(R.mipmap.action_like_accent));
} else {
holder.blogLikeBtn.setImageDrawable(context.getResources().getDrawable(R.mipmap.action_like_accent));
}
} else {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.blogLikeBtn.setImageDrawable(context.getDrawable(R.mipmap.action_like_gray));
} else {
holder.blogLikeBtn.setImageDrawable(context.getResources().getDrawable(R.mipmap.action_like_gray));
}
}

}
});
holder.blogLikeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {

if(!task.getResult().exists()){

Map<String, Object> likesMap = new HashMap<>();
likesMap.put("timestamp", FieldValue.serverTimestamp());

firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).set(likesMap);

} else {

firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").document(currentUserId).delete();

}

}
});
}
});
holder.blogCommentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent commentIntent = new Intent(context, CommentsActivity.class);
context.startActivity(commentIntent);

}
});
}else{

Toast.makeText(context, "Check Internet", Toast.LENGTH_SHORT).show();
}
}

Can I cancel previous Toast when I want to show an other Toast?

You need to call method on correct object.

toastObject.cancel()

How to disable a toast messages in my app?

You may have a third-party library that creates toasts.
You can use Find in path or Find usages in Android Studio to find where the toasts are being displayed.

How to stop showing Toast messages which are in queue in android

Final conclusion is: Toast in Que cannot be stoped . So avoid using toast in que.

How to avoid a Toast if there's one Toast already being shown

I've tried a variety of things to do this. At first I tried using the cancel(), which had no effect for me (see also this answer).

With setDuration(n) I wasn't coming to anywhere either. It turned out by logging getDuration() that it carries a value of 0 (if makeText()'s parameter was Toast.LENGTH_SHORT) or 1 (if makeText()'s parameter was Toast.LENGTH_LONG).

Finally I tried to check if the toast's view isShown(). Of course it isn't if no toast is shown, but even more, it returns a fatal error in this case. So I needed to try and catch the error.
Now, isShown() returns true if a toast is displayed.
Utilizing isShown() I came up with the method:

    /**
* <strong>public void showAToast (String st)</strong></br>
* this little method displays a toast on the screen.</br>
* it checks if a toast is currently visible</br>
* if so </br>
* ... it "sets" the new text</br>
* else</br>
* ... it "makes" the new text</br>
* and "shows" either or
* @param st the string to be toasted
*/

public void showAToast (String st){ //"Toast toast" is declared in the class
try{ toast.getView().isShown(); // true if visible
toast.setText(st);
} catch (Exception e) { // invisible if exception
toast = Toast.makeText(theContext, st, toastDuration);
}
toast.show(); //finally display it
}

Disabling notifications also disables toast on Oreo

Is this normal?

Yes ,this is normal behaviour (or a bug in android may be) .

better use SnackBar instead of Toast

Check this on Google issue tracker https://issuetracker.google.com/issues/36951147

Is it possible to disable Toasts or wait until toast disappears while testing

You can let Espresso wait until all toasts are disappeared with a custom idling resource.

Here I use CountingIdlingResource which is a idling resource managing a counter: when the counter changes from non-zero to zero it notifies the transition callback.

Here is a complete example; the key points follow:

public final class ToastManager {
private static final CountingIdlingResource idlingResource = new CountingIdlingResource("toast");
private static final View.OnAttachStateChangeListener listener = new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(final View v) {
idlingResource.increment();
}

@Override
public void onViewDetachedFromWindow(final View v) {
idlingResource.decrement();
}
};

private ToastManager() { }

public static Toast makeText(final Context context, final CharSequence text, final int duration) {
Toast t = Toast.makeText(context, text, duration);
t.getView().addOnAttachStateChangeListener(listener);
return t;
}

// For testing
public static IdlingResource getIdlingResource() {
return idlingResource;
}
}

How to show the toast:

ToastManager.makeText(this, "Third", Toast.LENGTH_SHORT).show();

How to set-up/tear-down a test:

@Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
Espresso.registerIdlingResources(ToastManager.getIdlingResource());
getActivity();
}

@After
public void tearDown() throws Exception {
super.tearDown();
Espresso.unregisterIdlingResources(ToastManager.getIdlingResource());
}


Related Topics



Leave a reply



Submit