0

Is there a way to check if there is a toast message being displayed in android? I am writing an idlingResource and I want to make sure no toast is currently being displayed before I return the resource as being idle.

E_K
  • 2,159
  • 23
  • 39

1 Answers1

0

Create Custom toast.

private Toast myToast = nuul; // Create class Level Object to

Add this code in the method

String message = "Your Message";
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
     the                    (ViewGroup) findViewById(R.id.custom_toast_container));

TextView text = (TextView) layout.findViewById(R.id.custom_toast_text);
text.setText(Message);

myToast = new Toast(getApplicationContext());
myToast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
myToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
myToast.setDuration(Toast.LENGTH_LONG);
myToast.setView(layout);
myToast.show();

custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp"
    android:background="#80000000">

    <TextView
        android:id="@+id/custom_toast_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:enabled="true"
        android:textSize="16dp"
        />
</LinearLayout>

Dismiss the Toast, if it is not null

 if (myToast != null) 
  myToast.cancel(); // Dismiss the toast
jessica
  • 1,700
  • 1
  • 11
  • 17
  • Thanks for the answer but in my case, I need to be able to detect system wide toasts I.e toasts produced even by other apps too – E_K May 21 '18 at 05:16
  • Do you have the id of toasts, which are produced in other apps? – jessica May 21 '18 at 05:44
  • do you check the comment by LieForBananas below your question? I thick it will help you. – jessica May 21 '18 at 10:27