mesutd0nmez
3/25/2017 - 11:54 AM

Custom Toast Message

Custom Toast Message

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:id="@+id/layout_toast"
    android:layout_height="match_parent"
    android:paddingLeft="15dp"
    android:paddingRight="15dp">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/login_input"
        android:paddingLeft="15dp"
        android:paddingRight="15dp">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/tvMessage"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:textAlignment="center"
            android:textColor="@color/darker"
            android:textSize="12dp" />
    </LinearLayout>
</LinearLayout>
public class IToast {
    private LayoutInflater layoutInflater;
    private View lyToast;
    private TextView tvMessage;

    public IToast(Activity activity, String message) {
        if(!activity.isFinishing()) {
            layoutInflater = activity.getLayoutInflater();
            lyToast = layoutInflater.inflate(R.layout.layout_toast, (ViewGroup) activity.findViewById(R.id.layout_toast));

            tvMessage = (TextView) lyToast.findViewById(R.id.tvMessage);
            tvMessage.setTypeface(Storage.helvaticaRegular);
            tvMessage.setText(message);

            Toast customToast = new Toast(activity.getApplicationContext());
            customToast.setGravity(Gravity.BOTTOM, 0, 25);
            customToast.setDuration(Toast.LENGTH_LONG);
            customToast.setView(lyToast);
            customToast.show();
        }
    }
}