Create Custom Toast in Android

Today i give you attractive look to your android project by using custom toast.Toast alert is a notification message that display for certain amount of time, and automatically 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 service,broadcast receiver,getting data from server...etc.

In this example we're creating a custom toast alert.

How to Create Custom Toast in Android,Create Custom Toast in Android,Custom Toast in Android,Toast in Android,

android.widget.Toast class used to create toast alert message.
Normal Toast which we use in our android program :
//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();
Now we'll do something different and remove old fashion Toast.
How to Create Custom Toast in Android,Create Custom Toast in Android,Custom Toast in Android,Toast in Android,

We're using:

  1. toast.xml file for toast layout.
  2. toast_border.xml file for toast style.
toast_border.xml
<?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>
toast.xml
<?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>
main_activity.xml
<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>
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();
         
    }
 
}

Thats done and comment for more posts !!!! 

No comments:

Post a Comment