Toast tutorial in Android

Basically , toast is important when you want to display some message to user or to give some notify message whenever user going to wrong direction or going to right direction too. It is also used for when task is completed or done or something getting error.

Lets discuss first , what is the structure in toast.There are two method we can display toast in android, one is programmatically or other is making custom layout.

1. Programmatically display toast method

Toast.makeText(getActivity(), "This is  the Toast message!",    Toast.LENGTH_LONG).show();

 in toast there is two time duration when can toast display in android;

int LENGTH_LONG Show the view or text notification for a long period of time.
int LENGTH_SHORT Show the view or text notification for a short period of time.This is the default.

2.Customize your toast

showToast(getActivity(), "Done");

and function like

public static void showToast(Activity acivity,  String stringToast) {
// TODO Auto-generated method stub

// TODO Auto-generated method stub

// get the LayoutInflater and inflate the custom_toast layout
LayoutInflater inflater = acivity.getLayoutInflater();
View layout = inflater.inflate(R.layout.customtoast, (ViewGroup) acivity.findViewById(R.id.toast_layout_root));

// get the TextView from the custom_toast layout
TextView text = (TextView) layout.findViewById(R.id.custom_toast_message);
text.setText(stringToast);

// create the toast object, set display duration,
// set the view as layout that's inflated above and then call show()
Toast toast = new Toast(acivity);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(layout);
toast.show();

}


Layout looks like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/appcolor"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/custom_toast_image"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/app_name"
        android:src="@drawable/icon" />

    <TextView
        android:id="@+id/custom_toast_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/custom_toast_image"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:contentDescription="content display"
        android:text=""
        android:textSize="15sp"
        android:textColor="@color/white" />

</RelativeLayout>

and are you want to more cusomize and learn more about toast , please go to http://developer.android.com/reference/android/widget/Toast.html

Happy Coding :)


Toast tutorial in Android Toast tutorial in Android Reviewed by D Kshetri on 9:15 PM Rating: 5

No comments:

Powered by Blogger.