I would like to display some information to the user, but I don't want the information to dismiss until the user taps elsewhere or presses the back button. I realize the obvious option for this is to display the text in a dialog (as opposed to a toast). I would like my dialog to resemble the system Toast. I recognize that I can just copy the transient_notification.xml
layout (and its relevant resources), but because Toast styling varies widely by device and OS, this is unlikely to produce a good match.
So, is there a good way to create a Dialog which inherits the styling for the system Toast?
Instantiate a Toast objectUse the makeText() method, which takes the following parameters: The application Context . The text that should appear to the user. The duration that the toast should remain on the screen.
Snackbars contain a single line of text directly related to the operation performed. They may contain a text action, but no icons. Toasts (Android only) are primarily used for system messaging. They also display at the bottom of the screen, but may not be swiped off-screen.
LENGTH_LONG for displaying the Toast. Using this, a Toast can be displayed for 3.5 seconds. In runtime, we will call a Toast for 3.5 seconds and cancel it at the 2nd second. We shall keep repeating this for a specific time defined inside the main code.
or you could use a custom layout
Custom method to display a toast
public static Toast currentToast;
/**
* Use a custom display for Toasts.
*
* @param message
*/
public static void customToast(String message) {
// Avoid creating a queue of toasts
if (currentToast != null) {
// Dismiss the current showing Toast
currentToast.cancel();
}
//Retrieve the layout Inflater
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Assign the custom layout to view
View layout = inflater.inflate(R.layout.custom_toast, null);
//Return the application context
currentToast = new Toast(context.getApplicationContext());
//Set toast gravity to center
currentToast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
//Set toast duration
currentToast.setDuration(Toast.LENGTH_LONG);
//Set the custom layout to Toast
currentToast.setView(layout);
//Get the TextView for the message of the Toast
TextView text = (TextView) layout.findViewById(R.id.text);
//Set the custom text for the message of the Toast
text.setText(message);
//Display toast
currentToast.show();
// Check if the layout is visible - just to be sure
if (layout != null) {
// Touch listener for the layout
// This will listen for any touch event on the screen
layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// No need to check the event, just dismiss the toast if it is showing
if (currentToast != null) {
currentToast.cancel();
// we return True if the listener has consumed the event
return true;
}
return false;
}
});
}
}
and the custom_toast.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:background="@drawable/custom_toast_shape">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/blue"
android:textSize="16sp"
android:text="@string/app_name"
/>
<View
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="@color/blue"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:maxEms="15"
android:gravity="center_horizontal"
android:id="@+id/text"
/>
</LinearLayout>
To use this just call
Utils.customToast("Just a test to see if toast is working!\nPerfect");
EDIT I have modified the method a little. Now it won't create a queue of toasts and it will be dismissed on touch, like the normal toast is.
If anyone else can improve it, please feel free to do it :)
You might want to try Crouton, its customizable and can be dismissed on touch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With