Wednesday 20 July 2011

Custom Toast

Lets build a better toast.
Put this in res/layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_serif"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10sp"
              android:background="@drawable/toast_border"
              >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:typeface="serif"
              android:textColor="#FFF"
              />
</LinearLayout>



.. and here is the background for the toast:
Put this in res/drawable



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

<shape      xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
    <stroke android:width="2sp" android:color="#efff" />
    <solid android:color="#cc444444"/>
    <padding android:left="20sp" android:top="2sp"
             android:right="20sp" android:bottom="2sp" />
    <corners android:radius="10sp" />
</shape>



and finally here is my Java code to call it:




public class CustomToast {

    // Warning: Only call maketToast() from inside the UI Thread
    public static Toast makeToast(Activity activity, int message) {
        return makeToast(activity, activity.getApplicationContext().getString(message));
    }

    public static Toast makeToast(Activity activity, String message ) {
        return makeToast(activity, message, Gravity.CENTER_VERTICAL, 0, 0);
    }

    public static Toast makeToast(Activity activity, String message, int gravity, int gravityX, int gravityY ) {
        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_serif,
                (ViewGroup) activity.findViewById(R.id.toast_serif));

        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText(message);
        Toast result = null;

        try {
            if (activity != null && activity.getApplicationContext() != null) {
                result = new Toast(activity.getApplicationContext());
                result.setGravity(gravity, gravityX, gravityY);
                result.setDuration(Toast.LENGTH_LONG);
                result.setView(layout);
            }
        } catch(NullPointerException npe) {
            Log.e("", "Activity terminated before we could make the CustomToast", npe);
        }
        return result;
    }

    public static void makeToastAndShow(final Activity activity, final String message) {
        activity.runOnUiThread(new Runnable() {
            public void run() {
                Toast toast = makeToast(activity, message);
                if (toast != null) {
                    toast.show();
                }
            }
        });
    }

    public static void makeToastAndShowLow(final Activity activity, final String message) {
        activity.runOnUiThread( new Runnable() {
            public void run() {
                Toast toast = makeToast(activity, message, Gravity.BOTTOM, 0, 50);
                if (toast != null) {
                    toast.show();
                }
            }
        });
    }
}



No comments:

Post a Comment