Android Toast Example with Custom View

By Arvind Rai, June 24, 2015
Android Toast is a way to display message for any operation. Message appears as popup for a short time and control still remains with parent layout. For example if we are doing any operation the toast message can be "Operation Completed" that will pop and disappear automatically. The popup layout takes the size required only for message. Android provides a class android.widget.Toast that shows the popup message. The popup message can also be a custom view. Here we will provide example for both normal and custom toast view.

Android Toast with Normal View

For the normal toast, we use Toast.makeText() method as follows.
CharSequence msg = "Hello Normal Toast!";
Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
toast.show();  

Android Toast with Custom View

For custom toast view, we need to create a layout in which we keep our image and text. Find the steps for creating custom view.
1. Create view :
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast_layout,(ViewGroup) findViewById(R.id.toast_layout_root));  
2. Configure message :
TextView txt = (TextView) layout.findViewById(R.id.toastmsg);
txt.setText("Hello Custom Toast!");  
3. Configure image :
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);  
4. Configure toast position and display :
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();  

Complete Example

Find the complete example now. We are creating two buttons, one for normal toast demo and second for custom toast view demo.
MainActivity.java
package com.concretepage.android;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
        OnClickListener listener = new OnClickListener() {
 			public void onClick(View view) {
                if (view.getId() == R.id.button1) {
     				CharSequence msg = "Hello Normal Toast!";
	 				Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
	 				toast.show();
                } else {
                	//create view
                	LayoutInflater inflater = getLayoutInflater();
                	View layout = inflater.inflate(R.layout.custom_toast_layout,(ViewGroup) findViewById(R.id.toast_layout_root));
                	//Configure message
                	TextView txt = (TextView) layout.findViewById(R.id.toastmsg);
                	txt.setText("Hello Custom Toast!");
                	//Set any image
                	ImageView image = (ImageView) layout.findViewById(R.id.image);
    				image.setImageResource(R.drawable.ic_launcher);
                	//Configure toast and display
                	Toast toast = new Toast(getApplicationContext());
                	toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                	toast.setDuration(Toast.LENGTH_LONG);
                	toast.setView(layout);
                	toast.show();
                }
 			}
        }; 
        Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(listener);
        Button btn2 = (Button)findViewById(R.id.button2);
        btn2.setOnClickListener(listener);
    }
}  
mylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#D1C4E9"
    android:orientation="vertical" >
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn1"/>
    
    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn2"/>
</LinearLayout>  
custom_toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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="10dp"
              android:background="#4CAF50">
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginRight="10dp"/>
    <TextView android:id="@+id/toastmsg"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFF"/>
</LinearLayout> 
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.concretepage.android"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" />
    <application
        android:allowBackup ="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity  android:name=".MainActivity" android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Concrete Page</string>
    <string name="btn1"> Normal Toast Demo</string>
    <string name="btn2"> Custom Toast Demo</string>
</resources> 

Output

Android Toast Example with Custom View

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us