вторник, 25 февраля 2014 г.

Android: custom Toast

XML layout


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/error_toast_background"
    android:gravity="center_vertical">

    <ImageView
        android:padding="@dimen/small_content_padding"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/toast_warning"/>

    <TextView
        android:id="@+id/toast_message"
        style="@style/FIS.Error.Toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/accept"/>
</LinearLayout>

Java


public static Toast createErrorToast(LayoutInflater inflater, Context context, String text) {

        Toast toast = new Toast(context);
        toast.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);

        View toastContent = inflater.inflate(R.layout.toast_content, null);
        assert toastContent != null;

        TextView message = (TextView) toastContent.findViewById(R.id.toast_message);
        assert message != null;

        message.setText(Html.fromHtml(text));

        toast.setView(toastContent);
        return toast;
    }

Комментариев нет:

Отправить комментарий