
Heads Up, Updates to toasts in Android 11Ĭustom toasts from the background are blocked, Android 11 protects (ViewGroup) findViewById(R.id.custom_toast_layout_id)) įor more help see how we Create custom Toast in Android: View layout = inflater.inflate(R.layout.custom_toast, LayoutInflater inflater = getLayoutInflater() STEP 2: In the Activity code, get the above custom view and attach to Toast: // Get your custom_toast.xml ayout Have written a builder class to simplify the above purpose Here is the link:įirst create a layout for a custom toast in res/layout/custom_toast.xml: In above code you can see, you can add image to TextView via setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) whichever position relative to TextView you want to.

tCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_fly, 0, 0, 0) TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message) * And now you can get the TextView of the default View of the Toast. View toastView = toast.getView() // This'll return the default View of the Toast. So below is what can you do to achieve this: Toast toast = Toast.makeText(this, "I am custom Toast!", Toast.LENGTH_LONG) So, if we have the resource id reference of that TextView, we can play with it. The default Toast's view contains a TextView for showing messages on it. If that is all, you want to do, then there is no need to make a separate layout and inflate it to the Toast instance. YouTube: Creating Custom Toast With Button in Android StudioĪ toast is for showing messages for short intervals of time So, as per my understanding, you would like to customize it with adding an image to it and changing size, color of the message text.

tGravity(Gravity.CENTER_VERTICAL, 0, 0) Īnd check out the below links also for a custom Toast.

Toast toast = new Toast(getApplicationContext()) TextView text = (TextView) layout.findViewById(R.id.text)

ImageView image = (ImageView) layout.findViewById(R.id.image) (ViewGroup) findViewById(R.id.toast_layout_root)) View layout = inflater.inflate(R.layout.toast_layout, MainActivity.java LayoutInflater inflater = getLayoutInflater()
