Day 4:Toast Notification in Android

Introduction

When we talk about "Toast" notification then, “Mobile Data Turned Off” , “Download Completed”comes to our mind. Yes, those small pop-ups are called Toast in Android.

What Toast Notification is

Toast Notofication is a Pop-up message that is shown on the screen for a while and hides itself after a while.

To implement this, we will use the Toast Class in our MainActivity.java file.

The agenda will be:

Toast Class

  • First, create a simple UI with a Button at least that raises the toast notification.
  • Then, code the setOnClickListner()
  • Inside, the listner we will place the Toast object.

Procedure

Step 1

Inside main_activity.xml, add a button that looks like: 

  1. <code>  
  2. <Button  
  3.        android:id="@+id/button1"  
  4.        style="@style/AppTheme"  
  5.        android:layout_width="match_parent"  
  6.        android:layout_height="wrap_content"  
  7.        android:layout_gravity="bottom"  
  8.        android:text="@string/buttonName" /> <!-- or better write "Click" -->>  
  9. </code>  
And, it looks like:

looks like

Note: Here, I have added a large TextView of no use (You can skip it). 

Step 2

In this step, we code the Button Click event, in other words setOnClickListner()

  1. <code>  
  2.       // Button Instance  
  3.       Button myButton = (Button) findViewById(R.id.button1);  
  4.   
  5.       myButton.setOnClickListener(new OnClickListener() {  
  6.   
  7. public void onClick(View arg0) {  
  8.   
  9.         // After taping on button1, we pop a Toast Message  
  10.        Toast toast=Toast.makeText(MainActivity.this"Hello C# Corner !!", Toast.LENGTH_SHORT);  
  11.        toast.setGravity(Gravity.CENTER, 0, 0); // Or, you can set Margin by: toast.setMargin(50,50);  
  12.        toast.show();  
  13.   
  14.   }  
  15.   });  
  16. </code>  
These lines of code are inside the onCreate() method. So, what these lines of code will do is, in the first line, we intiated the refrence of the button and linked it with the layout ‘s button, in other words button1.

Then, we populated a setOnClickListner() where we are puttingh the toast object.Inside. In the Toast class we have a makeText() method that takes three arguments.

The first is the current context, then the second is “Your Message” and the third is the time interval. Actually, the third one has two enum values, LENGTH_LONG and LENGTH_SHORT.

After deciding the makeText(), we move to setGravity that decides the postion of the toast message.And for the last we called the show() method that is easily understood. Instead of setGravity() you can use setMargin() that takes two margin values, the first is the horizontal margin then the vertical margin.

You can use any of the two, since they both work in the same context.

So, our Java code looks like: 

  1. <code>  
  2.    package com.greensyntax.toastapp;  
  3.   
  4.    import android.os.Bundle;  
  5.    import android.support.v7.app.ActionBarActivity;  
  6.    import android.view.Gravity;  
  7.    import android.view.View;  
  8.    import android.view.View.OnClickListener;  
  9.    import android.widget.Button;  
  10.    import android.widget.Toast;  
  11.   
  12.   
  13. public class MainActivity extends ActionBarActivity {  
  14.   
  15.    @Override  
  16. protected void onCreate(Bundle savedInstanceState) {  
  17.      super.onCreate(savedInstanceState);  
  18.      setContentView(R.layout.activity_main);  
  19.   
  20.      // Button Instance  
  21.      Button myButton = (Button) findViewById(R.id.button1);  
  22.   
  23.     myButton.setOnClickListener(new OnClickListener() {  
  24.   
  25. public void onClick(View arg0) {  
  26.   
  27.     // After taping on button1, we pop a Toast Message  
  28.     Toast toast=Toast.makeText(MainActivity.this"Hello C# Corner !!", Toast.LENGTH_SHORT);  
  29.     toast.setGravity(Gravity.CENTER, 0, 0); // Or, you can set Margin toast.setMargin(50,50);  
  30.     toast.show();  
  31.   
  32. }  
  33. });  
  34.   
  35. }  
  36. }  
  37. </code>  
And, finally the output will be like:

Output will

Conclusion

 If you encounter any problem with the code then just follow through the enclosed Eclipse-project file. 

Up Next
    Ebook Download
    View all
    Learn
    View all