Day 5: Improve Your Toast Message in Android App

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:

  • First, add a button and TextView on the main_activity.xml
  • Then, create a XML file for the toast. Because this time, we are inflating the XML file to toast notification.
  • Last, we will call it from setOnClickListner().

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 a XML file that acts as a toast popup when makeText() calls a toast instance.

Earlier, we had simplly 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.   
  8.    <ImageView  
  9.       android:id="@+id/customImage"  
  10.       android:layout_width="wrap_content"  
  11.       android:layout_height="wrap_content"  
  12.       android:contentDescription="@string/toastImage"  
  13.       android:src="@drawable/ic_launcher" />  
  14.   
  15.    <TextView  
  16.       android:id="@+id/customText"  
  17.       android:layout_width="wrap_content"  
  18.       android:layout_height="wrap_content"  
  19.       android:contentDescription="@string/toastImage"  
  20.       android:text="@string/toast_message" />  
  21. </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.   
  4.    myButton.setOnClickListener(new OnClickListener() {  
  5.   
  6.    public void onClick(View arg0) {  
  7.   
  8.       // Creating layout inflater instance  
  9.       LayoutInflater li= getLayoutInflater();  
  10.   
  11.       // Adding the XML file to inflator  
  12.       View view =li.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_layout));  
  13.   
  14.       // Creating Toast Object  
  15.       Toast myToast= new Toast(MainActivity.this);  
  16.       myToast.setDuration(myToast.LENGTH_LONG);  
  17.       myToast.setGravity(Gravity.CENTER, 00);  
  18.       myToast.setView(view);  
  19.       myToast.show();  
  20.    }  
  21. }); 

Inside setOnClickListner(), we created an inflater and a view. Then, added a custom_toast.xml file to the infaltor. 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.   
  12. public class MainActivity extends ActionBarActivity {  
  13.   
  14.    @Override  
  15.    protected void onCreate(Bundle savedInstanceState) {  
  16.       super.onCreate(savedInstanceState);  
  17.       setContentView(R.layout.activity_main);  
  18.   
  19.       // Button Instance  
  20.       Button myButton =(Button) findViewById(R.id.button1);  
  21.   
  22.       myButton.setOnClickListener(new OnClickListener() {  
  23.   
  24.       public void onClick(View arg0) {  
  25.   
  26.          // Creating layout inflater instance  
  27.          LayoutInflater li= getLayoutInflater();  
  28.   
  29.          // Adding the XML file to inflator  
  30.          View view =li.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_layout));  
  31.   
  32.          // Creating Toast Object  
  33.          Toast myToast= new Toast(MainActivity.this);  
  34.          myToast.setDuration(myToast.LENGTH_LONG);  
  35.          myToast.setGravity(Gravity.CENTER, 00);  
  36.          myToast.setView(view);  
  37.          myToast.show();  
  38.       }  
  39.    });  
  40.   
  41.    }  

And, it shows like:



Conclusion

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

Up Next
    Ebook Download
    View all
    Learn
    View all