Wednesday, January 25, 2017

How to create a custom toast in android


Decleration---------------------------------
LayoutInflater inflatert;
View layoutt;
TextView textt;
Toast toastt;


Initialization---------------------------------
inflatert = getLayoutInflater();
layoutt = inflatert.inflate(R.layout.custom_toast,
        (ViewGroup) findViewById(R.id.custom_toast_container));
textt = (TextView) layoutt.findViewById(R.id.text);
toastt = new Toast(getApplicationContext());
toastt.setGravity(Gravity.CENTER, 0, -150);
toastt.setDuration(Toast.LENGTH_LONG);
toastt.setView(layoutt);



Implementation---------------------------------
textt.setText("Invalid email address");
toastt.setView(layoutt);
toastt.show();


custome_toast xml file----------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/custom_toast_container"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:padding="8dp"    android:background="@color/default__toast_color"    >

    <TextView android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#FFF"        />
</LinearLayout>

No comments:

Post a Comment