over 9 years ago
Android give facility to change the view of a Toast. Sometimes we need to show message on a better way to user .
Here I am writing code to implement custom Toast. I make a separate layout for Toast . Here I am showing a ImageView and TextView on a toast and Inflate it on toast . We can set gravity and Orientation here.
custom_toast_layout.xml
- ?<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:layout_margin="20dp"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/custom_image"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:background="@drawable/user_icon"
- />
- <textview android:id="@+id/custom_text"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="Hello......Got it."
- android:layout_margintop="20dp"
- android:layout_marginleft="10dp"
- android:layout_torightof="@+id/custom_image">
- /RelativeLayout>
- View toastView=getLayoutInflater().inflate(R.layout.custom_toast_layout, null);
- Toast toastView=new Toast(this);
- customtoast.setView(toastView);
- customtoast.setGravity(Gravity.CENTER_HORIZONTAL);
- customtoast.setDuration(Toast.LENGTH_SHORT);
- customtoast.show();
?<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:layout_margin="20dp" android:orientation="vertical" > <ImageView android:id="@+id/custom_image" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/user_icon" /> <textview android:id="@+id/custom_text" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Hello......Got it." android:layout_margintop="20dp" android:layout_marginleft="10dp" android:layout_torightof="@+id/custom_image"> /RelativeLayout> View toastView=getLayoutInflater().inflate(R.layout.custom_toast_layout, null); Toast toastView=new Toast(this); customtoast.setView(toastView); customtoast.setGravity(Gravity.CENTER_HORIZONTAL); customtoast.setDuration(Toast.LENGTH_SHORT); customtoast.show();
0 Comment(s)