Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a clickable Toast-like notification?

I have a need to show a minimally-intrusive non-blocking notification which is not tied to the activity it is shown in (like a Toast) and which is clickable. Does anyone have any idea whether or not this is possible? Unfortunately, it appears that Toast notifications (custom or otherwise) are not clickable (i.e. setting an OnClickListener on its views has no effect). All the alternatives that I'm aware of (i.e. AlertDialog, PopupWindow and Crouton) seem to show a notification which is tied to the activity it is shown in (i.e. they won't continue showing when the activity finishes). Any suggestions?

like image 699
Adil Hussain Avatar asked Sep 05 '25 01:09

Adil Hussain


1 Answers

You can use PopupWindow, add an onClickListener and add a handler to auto cancel it after n times (just like the behavior of a toast). Something like this:

public static void showToast(Activity a, String title, String message) {

    // inflate your xml layout
    LayoutInflater inflater = a.getLayoutInflater();
    View layout = inflater.inflate(R.layout.custom_toast,
            (ViewGroup) a.findViewById(R.id.toast_layout_root));

    // set the custom display
    ((TextView) layout.findViewById(R.id.title)).setText(title);
    ((TextView) layout.findViewById(R.id.message)).setText(message);

    // initialize your popupWindow and use your custom layout as the view
    final PopupWindow pw = new PopupWindow(layout,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, true);

    // set windowType to TYPE_TOAST (requires API 23 above)
    // this will make popupWindow still appear even the activity was closed
    pw.setWindowLayoutType(WindowManager.LayoutParams.TYPE_TOAST);
    pw.showAtLocation(layout, Gravity.CENTER | Gravity.TOP, 0, 500);

    // handle popupWindow click event
    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // do anything when popupWindow was clicked
            pw.dismiss(); // dismiss the window
        }
    });

    // dismiss the popup window after 3sec
    new Handler().postDelayed(new Runnable() {
        public void run() {
            pw.dismiss();
        }
    }, 3000);
}

xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#000"
              android:orientation="vertical"
              android:elevation="10dp"
              android:padding="20dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#FFF"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#FFF"/>

</LinearLayout>
like image 77
mgcaguioa Avatar answered Sep 06 '25 14:09

mgcaguioa