Introduction

In my last article, we created a Toast Message that is quite basic. If you want to add an image in a Toast message then that is what we are going to do in this article, in other words, customize a Toast Message.
The agenda we will follow is the same as we did in the previous article as in the following:
The following is the procedure to follow.
Step 1: First, add a button and textView to the main_activity.xml layout.
Step 2: We will now add an XML file that acts as a toast popup when makeText() calls a toast instance.
Earlier, we had simply es a simple string as a toast message, but this time we will inflate a XML file as the toast's content.
So, we will make an additional XML file named custom_toast.xml as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. android:gravity="center" >
  7. <ImageView
  8. android:id="@+id/customImage"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:contentDescription="@string/toastImage"
  12. android:src="@drawable/ic_launcher" />
  13. <TextView
  14. android:id="@+id/customText"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:contentDescription="@string/toastImage"
  18. android:text="@string/toast_message" />
  19. </LinearLayout>
And in string.xml file, we have declared some new:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">Customized Toast</string>
  4. <string name="hello_world">Hello world!</string>
  5. <string name="action_settings">Settings</string>
  6. <string name="toastImage">My Toast</string>
  7. <string name="toast_message">This is my Custom Toast</string>
  8. </resources>
Step 3
  1. // Button Instance
  2. Button myButton =(Button) findViewById(R.id.button1);
  3. myButton.setOnClickListener(new OnClickListener() {
  4. public void onClick(View arg0) {
  5. // Creating layout inflater instance
  6. LayoutInflater li= getLayoutInflater();
  7. // Adding the XML file to inflator
  8. View view =li.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_layout));
  9. // Creating Toast Object
  10. Toast myToast= new Toast(MainActivity.this);
  11. myToast.setDuration(myToast.LENGTH_LONG);
  12. myToast.setGravity(Gravity.CENTER, 0, 0);
  13. myToast.setView(view);
  14. myToast.show();
  15. }
  16. });
Inside setOnClickListner(), we created an inflater and a view. Then, added a custom_toast.xml file to the inflator. Now, we identify the entire XML file as a single view.
Finally, we target a Toast object and assign all the desired properties along with the setView() method where we our view (that we have inflated).
The final code will be like:
  1. package com.greensyntax.customizedtoast;
  2. import android.os.Bundle;
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.view.Gravity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import android.widget.Toast;
  11. public class MainActivity extends ActionBarActivity {
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. // Button Instance
  17. Button myButton =(Button) findViewById(R.id.button1);
  18. myButton.setOnClickListener(new OnClickListener() {
  19. public void onClick(View arg0) {
  20. // Creating layout inflater instance
  21. LayoutInflater li= getLayoutInflater();
  22. // Adding the XML file to inflator
  23. View view =li.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_layout));
  24. // Creating Toast Object
  25. Toast myToast= new Toast(MainActivity.this);
  26. myToast.setDuration(myToast.LENGTH_LONG);
  27. myToast.setGravity(Gravity.CENTER, 0, 0);
  28. myToast.setView(view);
  29. myToast.show();
  30. }
  31. });
  32. }
  33. }
And, it shows like:

Conclusion

If you experience any issue then go for the enclosed file.