A toast provides simple feedback about an operation in a small popup.
It only fills the amount of space required for the message and the current activity remains visible and interactive.
For example, navigating away from an email before you send it , triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.
Normal Toast :
//display in short period of time Toast.makeText(getApplicationContext(), "Hello... I'm a short Toast!", Toast.LENGTH_SHORT).show();
//display in long period of time Toast.makeText(getApplicationContext(), "Hello... I'm a long Toast!", Toast.LENGTH_LONG).show();
Custom Toast :
You can create the layout for the custom Toast with the following XML ( saved
as custom_toast_layout.xml ) :-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8dp" android:background="#DAAA" > <ImageView android:src="@drawable/droid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /> </LinearLayout>
Now create a method as below , to call the custom toast :-
private void showCustomToast(){ LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("This is a custom toast"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); }
Happy coding :)
********************************************************************************