Dialogue And Toast In Android

Dialogue and Toast in Android

 
Here I am going to describe to you the most commonly used two controls of Android application. If the android application needs to confirm anything from the user then he can use a Dialogue with two buttons asking for Yes or No permissions. If the android application wanted something for the user for a short period of time, then we can use Toast.
 
Both are showing some information to the user but the main difference is that Dialogue will not dismiss if the user does not select the Yes or No button. But the Toast will be shown in the UI for some amount of time, the user doesn’t need to interact with the Toast.
 
In this article, I am going to show you how to create a project in android with Android Studio and how to add UI controls in the layout and how we can use that controls in the Java file. And of course how to show dialogue and Toast.
 
Please follow the steps
 
Step 1
 
Open Android Studio and click File->New->New project and a window will appear on the screen, 
 
 
 
Step 2
 
Here you can add a project name and location for that project and click the Next button then another window will appear and you can select the Minimum SDK that you want to create. And then in the next window, you can select the template you like from a list of predefined templates and click Finish.
 
Now your project will be created in Android Studio. And you will have some folders like res, src, manifest like that these are the files needed for Android application. In which your java files will be in the src folder and layout will contain in the res folder.
 
Now open the res->layout->activity_main.xml file, there you will see two options at the left bottom of the screen, they are Design and Text. With the Design mode, you can drag and drop controls in the UI. Then drag two buttons in the layout and give appropriate text and id to both of the buttons.
 
Now go to the MainActivity.java file and load the XML file to java using setContentView in the onCreate() method. Now load your XML resources into java variable using the following code.
  1. Button showDialogueButton = (Button) findViewById(R.id.showDialogueButton);  
  2. Button showToastButton = (Button) findViewById(R.id.showToastButton);  
The id should be the same that you use in the layout file.
 
Then add click listeners for the two buttons. For showing dialogue we need to use AlertDialogue.Builder class in Android. There are some methods to set the message that we need to show to the user and options for adding Positive and Negative buttons, like the following,
 
Step 3
  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);    
  2. builder.setMessage("This is a dialogue");    
  3. builder.setPositiveButton("Yes"new DialogInterface.OnClickListener()     
  4. {    
  5.     @Override public void onClick(DialogInterface dialogInterface, int i) {}    
  6. });    
  7. builder.setNegativeButton("No"new DialogInterface.OnClickListener()    
  8. {    
  9.     @Override public void onClick(DialogInterface dialogInterface, int i) {}    
  10. });    
  11. builder.show();   
    Don't forget to call builder.show(); this method is used for showing the dialogue to the user.
     
    Step 4
     
    Now add click listener for show toast button click like the following,
    1. showToastButton.setOnClickListener(new View.OnClickListener()    
    2. {    
    3.     @Override public void onClick(View view)    
    4.     {    
    5.         Toast.makeText(MainActivity.this"This is a Toast", Toast.LENGTH_SHORT).show();    
    6.     }    
    7. });   
     
    For this, we can use Toast class, and it has a static method named makeText which will accept three parameters context, message, and duration of the toast to show in front of the user.
     
    Please see the output also,
     
     
    Read more articles on Android