Custom toast in android

A toast message is a way to alert users about what is happening in your Android app without interrupting the UX. Here is the example usage of the toast in the android application.

When we register new user once entered all the register details like username, password, and email id, after hitting the submit button it will try to register the user.

If the register is successful it shows the success toast message else it will show the error toast message. But it will not affect the UI.

This is the sample program for how to create custom toast in the android studio.


Related Post

how to load image from sdcard in android

Custom dialog in android studio example

customize listview android


Please follow the steps to create the custom toast in the android.

  • Create layout for the custom view.
  • Inflate layout and create view.
  • Create Toast() and set the view.
  • Set the gravity and show the Toast.

Step 1 – Create layout for the custom view

You need to create the separate layout for the custom toast layout. This is how the toast looks like.

custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </RelativeLayout>
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/relativeLayout2"
        android:src="@drawable/ic_launcher" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginLeft="33dp"
        android:layout_toRightOf="@+id/imageView1"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

Step 2 – Inflate layout and create view

Using the LayoutInflater create a view for your Toast layout.

LayoutInflater inflater=getLayoutInflater();
View myview=inflater.inflate(R.layout.custom_toast, (ViewGroup)findViewById(R.id.RelativeLayout1));

declare all the views like textview and button in the custom toast layout.

ImageView toastimage=((ImageView)myview.findViewById(R.id.imageView1));
TextView toasttext=((TextView)myview.findViewById(R.id.textView1));

Step 3 – Create Toast() and set the view.

Create the Toast class object and set the custom toast view to the toast object.

Toast toast=new Toast(getApplicationContext());
toast.setView(myview);

Step 4 – Set the gravity and show the Toast

Then, Set the gravity for the toast, where you need to show the custom toast.and also set the duration for the toast.

toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.setDuration(4);

Finally, show the custom toast by calling show() of the Toast class.

toast.show();

Custom Toast Example

main.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"
    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/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="ShowToast" />
</RelativeLayout>

ToastActivity.Java

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
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 Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 Button Showtoast=(Button)findViewById(R.id.button1);
 Showtoast.setOnClickListener(new OnClickListener() 
 {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 LayoutInflater inflater=getLayoutInflater();
 View myview=inflater.inflate(R.layout.custom_toast, (ViewGroup)findViewById(R.id.RelativeLayout1));
 ImageView toastimage=((ImageView)myview.findViewById(R.id.imageView1));
 TextView toasttext=((TextView)myview.findViewById(R.id.textView1));
 toasttext.setText("Hello Friends");
 toastimage.setBackgroundResource(R.drawable.ic_launcher);
 Toast toast=new Toast(getApplicationContext());
 toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
 toast.setDuration(4);
 toast.setView(myview);
 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;
 }
}
Download

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top