Android Alert Tips Do you really know dialog, toast and snackbar? _android

Source: Internet
Author: User

Dialog and toast all people will not be unfamiliar, this we usually use is too much. And Snackbar is the new control provided in the Design support library, some friends may have used it, some friends may not have to understand. But do you really know when you should use dialog, when should you use toast, and when should you use Snackbar? In this article we'll take a look at the timing of these three, plus some additional tips.

1. Dialog

First of all, to introduce the use of dialog, in fact, very simple, I believe most people are often used:

Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.settitle ("Title")
. Setmessage ("Dialog content.")
. Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
@Override public
void OnClick ( Dialoginterface dialog, 
int which) {
}
})
. Setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override public
void OnClick (Dialoginterface dialog,
int which) {
}
})
. Show ();

This code can pop up a very fine dialog, as shown in the following figure:

Now this dialog is material design style, because I am running on the 6.0 system, so it will automatically give this style. But what would it be like to run on an older version of the system, say 2.3? Run it and you'll see, as shown in the following figure:

Amount of ... This effect is more ugly, but there is no way, this is the style of the 2.3 system at that time.

People's aesthetic is always improving, do we have any way in the old version of the system also use Material Design style dialog? Of course, Google has fully taken this into account, and also provides a Alertdialog class in the Appcompat-v7 library, with the full path:

Android.support.v7.app.AlertDialog

We use the Alertdialog in this package to keep the dialog box in the same style as all the system versions. Now rerun in the 2.3 system, the effect looks like this:

Can see, now the effect is quite good, this is a small skill.

The role of dialog is to give the user a hint and let the user make a judgment according to the prompts. The dialog feature is that it will stop you from doing what you were doing, and you have to stop working on dialog. However, most people may not like to be interrupted, perhaps the user is dealing with an important operation, suddenly pop a dialog blocked his original operation, this time users will become very annoyed.

Therefore, it is better to be cautious when using dialog, and try not to bring a bad experience to the user.

2. Toast

Speaking of not blocking what the user is doing, this extends to our second topic today, Toast. Toast will only pop up a piece of information, tell the user So-and-so things have happened, after a period of time will automatically disappear. It does not block any user action at all, and even the user can completely ignore toast.

So let's take a look at the basic usage of toast, as follows:

Toast.maketext (Context, "things happened", Toast.length_short). Show ();

The last parameter is used to specify the length of the Toast display, Toast.length_short indicates a shorter display time, and Toast.length_long indicates a longer display time.

However, it is not said that the use of toast is not a bit of depth, for example, the above wording will have the following picture of the problem:


As you can see, here I clicked the button five times in a row, and the toast triggered five times. This experience is not good, because perhaps the user is shaking a little bit more than a few times, resulting in toast for a long time off. Or we are in fact already doing other operations, should pop up the new toast prompt, but the last one toast has not shown the end.

Therefore, the best practice is to encapsulate the toast call into an interface, written in a common class, as follows:

public class Util {
private static Toast Toast;
public static void Showtoast (context context, 
String content) {
if (toast = = null) {
toast = Toast.maketext (co ntext,
content, 
toast.length_short);
} else {
toast.settext (content);
}
Toast.show ();
}

Can see, here and we usually use the way of toast is not the same, here will first determine whether the toast object is empty, if it is empty will invoke the Maketext () method to generate a Toast object, otherwise directly call the SetText () method to set the displayed content, Finally, call the show () method to display the toast. Because the new Toast object will not be generated every time the call is made, the problem we have just encountered will not appear here.

It's also simple to call, just pass the context object and toast to the display:

Util.showtoast (Context, "things happened");

Now let's run through the program again, as shown in the following image:

As you can see, no matter how many times we trigger the toast call, it's only going to last one toast the length of the display, which is a little tricky.

Toast's role is to tell the user what happened now, will not block the user's operation, but at the same time the user can only passively receive this thing, because there is no way to let users choose to agree or reject.

Although toast is better than dialog in terms of user experience, it should be used with caution, especially when it comes to sensitive operations. For example, the deletion of data, only to give users a hint: "Your data has been deleted," but not to give the user to choose whether to delete the opportunity, this time the user may be going to burst away.

3. Snackbar

If dialog and toast are two extremes, then Snackbar is in the middle. Snackbar and toast are similar, but more versatile, and they can interact with the user. Snackbar uses an animated effect to bounce out of the bottom of the screen and automatically disappears after a period of time.

Before using Snackbar, you first need to add the appropriate dependencies in the App/build.gradle:

dependencies {
compile ' com.android.support:design:23.4.0 '
}

Then you can use Snackbar, which is similar to the toast:

Snackbar.make (view, "Data deleted", Snackbar.length_long)
. Setaction ("Undo", new View.onclicklistener () {
@ Override public
void OnClick (View v) {
}
})
. Show ();

This calls the Snackbar make () method to create a Snackbar object, the first parameter of the make () method needs to pass in a view, as long as any view of the current interface layout can be Snackbar will use this view to find the outermost layout to display the Snackbar. The second parameter is what is displayed in the Snackbar, and the third is the length of time the snackbar displays. These are similar to toast.

A setaction () method is then invoked to set an action so that Snackbar is more than just a hint, but can interact with the user. Finally, the show () method is called to display the Snackbar.

Now rerun the program, as shown in the following illustration:

As you can see, the Snackbar effect is a bit like toast, but it bounces off the bottom of the screen. In addition, Snackbar can add a button that interacts with the user, such as deleting the data to give the user an option to undo, from these small details can enhance a lot of user experience.

4. Summary

Now that you have three ways to give users a hint, Dialog, toast and Snackbar, here's a summary of the timing of these three ways.

Dialog: Use Dialog when the message is critical and the user must make a decision to continue.

Toast: When the cue message just tells the user that something has happened and the user does not need to respond to the event, use Toast.

Snackbar: Any of the above scenarios, Snackbar may be your best choice.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.