

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:
ANDROID STUDIO TOAST IMAGE CODE
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.
ANDROID STUDIO TOAST IMAGE ANDROID
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()
