Friday, 10 May 2013

custom toast android example

custom toast android example

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 user name, password and email id, after hitting the submit button it will try to register the user.


If the register is successful it show 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 android studio.


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



1. First you need to create the separate layout for the custom toast layout. This is for 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>

2. 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));

3. 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));


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

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

5. 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);

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


toast.show();
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;
 }

}


No comments: