blog

Home / DeveloperSection / Blogs / Create custom toast notification in android

Create custom toast notification in android

Manoj Pandey2553 17-Jul-2015

In Android, Toast is a notification message that pop up, display a certain amount of time, and automatically fades in and out.

Generally it is use for to show error, warnings or success notification for user.

A toast is a view containing a quick little message for the user. The toast class helps you create and show those.

When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two examples are the volume control, and the brief message saying that your settings have been saved.

Basically we are use below code to show toast notification in android.

     Toast.makeText(MyActivity.this,
                                "My Toast Notification",
                                Toast.LENGTH_SHORT).show();

 Here I am telling you to create custom notification. You can add buttons, textview, imagview etc in toast layout.

Follow below code to show custom views in toast notification.

1.      Create an android project in eclipse or Android studio.

2.      Add two button for show normal toast and custom toast in
activity_main.xml file

 

 <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:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <Button

        android:id="@+id/btnNormalToast"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="10sp"

        android:text="Show Normal Toast" />

 

    <Button

        android:id="@+id/btnCustmToast"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="10sp"

        android:text="Show Custom Toast" />

 

</LinearLayout>

 

3.   Add Create custom layout for show toast notification name as my_custom_layout.xml file 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#666633"

    android:gravity="center"

    android:orientation="vertical" >

 

    <ImageView

        android:layout_width="60sp"

        android:layout_height="60sp"

        android:layout_gravity="center"

        android:src="@drawable/ic_launcher" />

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:text="  Hi am using custom notification  "

        android:textColor="#FFFFFF"

        android:textSize="17sp" />

 

</LinearLayout>

 

4.     Add following code in MainActivity.class 

package com.example.toastdemo;

 

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

 

public class MainActivity extends Activity {

     Button btnCustom, btnNormal;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

           btnCustom = (Button) findViewById(R.id.btnCustmToast);

           btnNormal = (Button) findViewById(R.id.btnNormalToast);

           btnNormal.setOnClickListener(new OnClickListener() {

 

                @Override

                public void onClick(View v) {

                     // TODO Auto-generated method stub

               Toast.makeText(MainActivity.this,

                              "Hi am using normal toast notification",

                                Toast.LENGTH_SHORT).show();

 

                }

           });

           btnCustom.setOnClickListener(new OnClickListener() {

 

                @Override

                public void onClick(View v) {

 

               // create object of toast class

        Toast toast = new Toast(MainActivity.this);

          // set time out of toast notification

 

            toast.setDuration(Toast.LENGTH_LONG);

       // set gravity of toast notification. It will be show in center

       // of the screen.

            toast.setGravity(Gravity.CENTER, 0, 0);

 

      // create Layout inflater for add layout on view

          LayoutInflater inflater = (LayoutInflater) MainActivity.this

                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 

      // add layout on views

         View view = inflater.inflate(R.layout.my_custom_toast, null);

 

      // add view on toast view

         toast.setView(view);

 

      // show toast

         toast.show();

 

                }

           });

     }

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

} 



 

Now run you application

Create custom toast notification in android 


See normal notification 

Create custom toast notification in android

See custom notification 

Create custom toast notification in android

 

 


Updated 13-Mar-2018

Leave Comment

Comments

Liked By