Create a custom toast message in android :
Step 1 : Create <font color="#FF00B3">mytoast.xml</font> file in <font color="#FF00B3">res/layout/</font> to define the layout of our custom toast. Place the following code in the mytoast.xml file or design your required layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toast_border">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image0" />
<TextView
android
addingLeft="10dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The image has been uploaded to server."
android:layout_gravity="center_vertical" />
</LinearLayout>
Step 2 : Create a <font color="#FF00B3">toast_border.xml</font> file in <font color="#FF00B3">res/drawable/</font> and place the following code in this file. This designs the border of our custom toast.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="4dp" android:color="#FFFFFFFF" />
<padding android:left="7dp" android:top="7dp"
android:right="7dp" android:bottom="7dp" />
<corners android:radius="4dp" />
</shape>
Step 3 : Now use the following function in your activity to generate the custom toast. Make a call to the function to generate the toast.
public void showCustomToast()
{
Context context = getApplicationContext();
// Create layout inflator object to inflate toast.xml file
LayoutInflater inflater = getLayoutInflater();
// Call mytoast.xml file for toast layout
View toastRoot = inflater.inflate(R.layout.mytoast, null);
Toast toast = new Toast(context);
// Set layout to toast
toast.setView(toastRoot);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
Note : You can customize the "showCustomToast" function with parameters to send in dynamic values and images as per your requirement.