Search code examples
androidandroid-buttontoast

Toast with button in Android


How to implement toast with button as shown below? It is used in recent Google apps in Android.

enter image description here


Solution

  • You can use custom toast messages like this. This is a dialog will dismiss after 2.5 seconds just like a toast.

    public class CustomToast extends Dialog {  
     public CustomToast(Context context, String text) {  
      super(context);  
      requestWindowFeature(Window.FEATURE_NO_TITLE);  
      LayoutInflater inflater = (LayoutInflater) context  
      .getSystemService(android.content.Context.  
         LAYOUT_INFLATER_SERVICE);  
      
      View layout = inflater.inflate(R.layout.toast, null);  
      TextView tv = (TextView) layout.findViewById(R.id.toastText);  
      tv.setText(text);    
      setContentView(layout);  
      show();  
      setCanceledOnTouchOutside(true);  
      setCancelable(true);  
      Window window = getWindow();  
      window.setGravity(Gravity.BOTTOM);  
      new Handler().postDelayed(new Runnable() {  
      
       public void run() {  
        dismiss();  
       }  
      }, 2500);  
     }  
    }