Monday, March 4, 2013

Android Customized Toast

                                                ANDROID CUSTOMIZED TOAST

If you want to warn the user or give an information briefly to the user we use toast most of time. Default toast is not enough for some of the situations so you can make your own toast such as showing images in toast or through styled writings. For example lets create a customized toast :

1 ) Create an xml file into the res-->layout folder of your android project give name as customized.xml

!! You can define relativelayout, linearlayout, tablelayout... whatever you want... And drag an imageview into the folder and choose your image from the menu lastly your xml should look like below.

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sdcard" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:text="@string/pleaseinsertyoursdcard"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
@drawable/sdcard you can open a folder as drawable into the res folder and put a picture named as sdcard into that folder.
Then come to your own activity class file that you want to show toast in it.

If you are in class that is extended from Activity you can code such below


 Toast toast = new Toast(getApplicationContext());
                  toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                  toast.setDuration(Toast.LENGTH_LONG);
                  LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  toast.setView(inflater.inflate(R.layout.plugsdcard,null));
                  toast.show();

Now your own toast file will be shown. You can change your layout xml according to your needs.

!!!! If you want to show your toast in a class file that is extended from BroadcastReceiver you can code such as below in onReceive(Context context, Intent intent) function:

Toast toast = new Toast(context);
//context is your app act. context equals to getApplicationContext in class that is extended from Activity class
                   toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                   toast.setDuration(Toast.LENGTH_LONG);
                   LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   toast.setView(inflater.inflate(R.layout.plugsdcard,null));
                   toast.show();


This small example can help your program being a little customized...

No comments:

Post a Comment