Use Crouton, it is a very flexible Toast library.
You can use it just like toasts:
Crouton.makeText(context, "YOUR_MESSAGE", Style.INFO);
or you can even go a little deeper and customise it more, like setting the time to infinite! for example here I want to show a toast message until the user acknowledges it by clicking on it.
private static void showMessage(final Activity context, MessageType type, String header, String message) {
View v = context.getLayoutInflater().inflate(R.layout.toast_layout, null);
TextView headerTv = (TextView) v.findViewById(R.id.toastHeader);
headerTv.setText(header);
TextView messageTv = (TextView) v.findViewById(R.id.toastMessage);
messageTv.setText(message);
ImageView toastIcon = (ImageView) v.findViewById(R.id.toastIcon);
final Crouton crouton = getCrouton(context, v);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Crouton.hide(crouton);
}
});
crouton.show();
}
private static Crouton getCrouton(final Activity context, View v) {
Crouton crouton = Crouton.make(context, v);
crouton.setConfiguration(new Configuration.Builder().setDuration(Configuration.DURATION_INFINITE).build());
return crouton;
}
Custome Layout that will be inflated for the toast.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="@drawable/shadow_container"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="@dimen/default_margin"
tools:ignore="Overdraw">
<ImageView
android:id="@+id/toastIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/default_spacing_full"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/toastHeader"
style="@style/ItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/toastMessage"
style="@style/ItemSubText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>