How to use Android Dialog,Toast, Snackbar

Dialog and Toast all certainly not unfamiliar, this we usually use is too much. Snackbar is a new control provided in the Design Support library, some friends may have been used, and some friends may not have to understand. But do you really know when to use Dialog, when should you use Toast, when should I use Snackbar? In this article, we will learn about the timing of the use of the three, in addition to introduce some additional skills.

Dialog and Toast all certainly not unfamiliar, this we usually use is too much. Snackbar is a new control provided in the Design Support library, some friends may have been used, and some friends may not have to understand. But do you really know when to use Dialog, when should you use Toast, when should I use Snackbar? In this article, we will learn about the timing of the use of the three, in addition to introduce some additional skills.

Dialog

First of all to introduce the use of Dialog it, 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 beautiful Dialog, as shown below:

20160717190208018

Now this Dialog is Material Design style, because I was running on the 6.0 system, it will automatically give such a style. But if the old version of the system to run, for example, 2.3 system, what will be the effect? Run to know, as shown below:20160717190240159.png

This effect is more ugly, but no way, this is the 2.3 system was the style of the way !

People’s aesthetic is always in progress, we have no way in the old version of the system also use Material Design style Dialog it? Of course, Google has taken full account of this, in the appcompat-v7 library also provides an AlertDialog class, the full path is:

android.support.v7.app.AlertDialog

 

We use this package AlertDialog, will be able to make the dialog box in all the system version to maintain a consistent style. Now re-run in the 2.3 system, the effect is as follows:

20160717190351520

You can see that the effect is now quite good, it can be considered a little trick it.

Dialog’s role is to give users a message, and let the user make a judgment based on the prompts. The characteristic of Dialog is that it will prevent you from doing what you are doing and you have to stop working on Dialog. However, most people may not like this is interrupted, perhaps the user is dealing with an important operation, suddenly pop up a Dialog to block his original operation, this time the user will become very annoying.

Therefore, the use of Dialog when the time is still cautious, try not to give users a bad sense of experience.

Toast

Speaking will not block the user’s ongoing operation, which extends to our second topic today, Toast. Toast will only pop up a message to tell the user that something has happened, after a period of time will automatically disappear. It will not block any user’s operation, and even the user can completely ignore the Toast.

So let’s take a look at the basic usage of Toast, as shown below:

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 that the display time is short and Toast.LENGTH_LONG indicates that the display time is longer.

But not to say that the use of Toast on the depth of a little, for example, the above-mentioned wording will exist as shown in the following figure:

20160717190636909.png

You can see, here I quickly click on the button five times, Toast triggered five times. This experience is really bad, because perhaps the user is a little more shaking a few times, resulting in Toast on a long time can not afford to shut down. Or we actually have other operations, and should pop up a new Toast prompt, and on a Toast but not show the end.

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

public class Util {

    private static Toast toast;

    public static void showToast(Context context, 
        String content) {
        if (toast == null) {
            toast = Toast.makeText(context,
                         content, 
                         Toast.LENGTH_SHORT);
        } else {
            toast.setText(content);
        }
        toast.show();
    }

}

 

You can see here and we usually use Toast way is not the same, here will first determine whether Toast object is empty, if it is empty will call makeText () method to generate a Toast object, or call directly SetText () method to set the display of the content, and finally call the show () method to Toast displayed. Since the new Toast object will not be generated every time it is called, the problem we have just encountered will not appear here.

Call the time is also very simple, only the Context object and Toast to display the contents of the incoming can be:

Util.showToast(context, "things happened");

Now we re-run the program again, the effect as shown below:

20160717190747269.png

You can see, now no matter how many times we trigger Toast call, will only continue Toast show the length of time, which can be considered a little trick it.

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 accept this thing, because there is no way to let the user choose to agree or refuse.

Although Toast in the user experience than Dialog better, but also with caution, especially when involved in some sensitive operations. For example, delete the data, only to the user a prompt: “your data has been deleted”, and not to the user to choose whether to delete the opportunity, this time the user may be runaway.

Snackbar

If Dialog and Toast are two extremes, then Snackbar is in the middle of the position. Snackbar and Toast are similar, but the use is more extensive, and it is able to interact with the user. Snackbar uses an animation effect from the bottom of the screen to pop out, after a period of time will automatically disappear.

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

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

 

And then you can use Snackbar, and its usage and Toast is more similar:

Snackbar.make(view, "data deleted",Snackbar.LENGTH_LONG)
        .setAction("Undo", new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                }
            })
        .show();

 

Here the call to Snackbar’s make () method to create a Snackbar object, make () method of the first parameter needs to be passed into a view, as long as the current interface layout of any view can be, Snackbar will use this view to automatically find the most The outer layout is used to show Snackbar. The second argument is what Snackbar displays, and the third argument is the length of the Snackbar display. These are similar to Toast.

Then there is a setAction () method to set an action, so that Snackbar is not just a hint, but can interact with the user. Finally call the show () method to let Snackbar be displayed.

Now re-run the program, the effect as shown below:

 

20160717191403222

You can see, Snackbar effect is somewhat similar to Toast, but it is from the bottom of the screen pop out. In addition Snackbar above can join and user interaction button, such as delete the data when the user to an Undo option, from these small details can enhance a lot of user experience.

sum up

Now you have three ways to give users tips, Dialog, Toast and Snackbar, the following we use the three ways to make a summary of the time it.

  1. Dialog: Use Dialog when the prompt message is critical and must be made by the user to make a decision.
  2. Toast: When the message tells the user that something has happened, the user does not need to respond to this matter when using Toast.
  3. Snackbar: Any of the other scenes above, Snackbar may be your best choice.
  1. I was very pleased to find this site. I wanted to thank you for your time just for this wonderful read!! I definitely liked every little bit of it and I have you book marked to check out new stuff in your website.

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: