Android Custom Toast Tutorial

                                     Android Custom Toast



1) Toast is used to show simple message to the user.
2) Toast doesn't take focus,and not having interaction.
3) Based on the text size, the toast occupy the space in a screen.
4) By default it displays in bottom of the device.

Step 1:

Copy the below xml file and paste it in main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_short_toast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Short Toast" />

    <Button
        android:id="@+id/btn_long_toast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Long Toast" />

    <Button
        android:id="@+id/btn_custom_toast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Custom Toast" />

</LinearLayout>

Step 2:

Create the custom_toast.xml file in layout folder. Then copy the below coding and paste in custom_toast.xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ff9977"
    android:orientation="horizontal"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/toast_image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="5dp"
       android:background="@drawable/ic_launcher"/>

    <TextView
        android:id="@+id/text_toast_message"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#00ffff"
        android:text="Custom Toast"
      />

</LinearLayout>

Step 3:

Here is the  CustomToastExample.java.Copy and paste the java coding into your project.

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CustomToastExample extends Activity {
    Button shortToast, longToast, customToast;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        shortToast = (Button) this.findViewById(R.id.btn_short_toast);
        longToast = (Button) this.findViewById(R.id.btn_long_toast);
        customToast = (Button) this.findViewById(R.id.btn_custom_toast);

        shortToast.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Show for 3 seconds",
                        Toast.LENGTH_SHORT).show();
            }
        });
       
        longToast.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Show for 5 seconds",
                        Toast.LENGTH_LONG).show();
            }
        });
       
        customToast.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custom_toast,
                        (ViewGroup) findViewById(R.id.custom_toast_layout_id));

                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
        });
    }
}






The complete Source code for Custom Toast Sample

Comments

Popular posts from this blog

Share Data Across Application in Android

Android Naming Conventions

MD5 Encryption in Android