Custom Toast Alert - Android Example

DOWNLOAD CODE

Related Examples

In this example creating a custom toast alert.

android.widget.Toast class used to create toast alert message.

Toast alert is a notification message that display for certain amount of time, and automtaically fades out after set time.

Use it to show alert message to user.

Use it for debugging your application.

Use toast alert message to show alert from background sevice,boadcast reciever,getting data from server...etc.

 

Normal Toast Alert :

 

//Show alert for short period of time
Toast.makeText(getApplicationContext(), "This is Toast example.", Toast.LENGTH_SHORT).show();
 
//Show alert for long period of time
Toast.makeText(getApplicationContext(), "This is Toast example.", Toast.LENGTH_LONG).show();

 

Custom Toast Alert :

 

Using toast.xml file for toast layout.
Using toast_border.xml file for toast style.

 

Project Structure :

Custom toast project sketch

 

 

File : res/drawable/toast_border.xml

Create style for custom toast layout.

 


    <?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>

 

File : res/layout/toast.xml

This file is used for custom toast layout.

See android:background="@drawable/toast_border" call toast_border.xml file.

 


    <?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:paddingLeft="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>


 

File : res/layout/activity_custom_toast.xml

Create main activity.

 


    <RelativeLayout 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"
        tools:context=".CustomToast" 
        android:background="#890000">
    
        <TextView 
            android:paddingTop="20px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        
        <Button
            android:id="@+id/Clickhere"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click here to see custom alert" 
            />
    
    </RelativeLayout>

 

File : src/CustomToast.java

 


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class CustomToast extends Activity {

	private Button Clickhere;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_custom_toast);
		
		Clickhere = (Button) findViewById(R.id.Clickhere);
		
        // Button click listner
		Clickhere.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				showCustomAlert();	

			}
		});
	}

	
	public void showCustomAlert()
	{
	    
		Context context = getApplicationContext();
        // Create layout inflator object to inflate toast.xml file
		LayoutInflater inflater = getLayoutInflater();
		 
        // Call toast.xml file for toast layout 
		View toastRoot = inflater.inflate(R.layout.toast, 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();
		
	}

}