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:
- 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:
- <code>
- <Button
- android:id="@+id/button1"
- style="@style/AppTheme"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="bottom"
- android:text="@string/buttonName" /> <!-- or better write "Click" -->>
- </code>
And, it 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().
- <code>
-
- Button myButton = (Button) findViewById(R.id.button1);
-
- myButton.setOnClickListener(new OnClickListener() {
-
- public void onClick(View arg0) {
-
-
- Toast toast=Toast.makeText(MainActivity.this, "Hello C# Corner !!", Toast.LENGTH_SHORT);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
-
- }
- });
- </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:
- <code>
- package com.greensyntax.toastapp;
-
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.view.Gravity;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
-
-
- public class MainActivity extends ActionBarActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
- Button myButton = (Button) findViewById(R.id.button1);
-
- myButton.setOnClickListener(new OnClickListener() {
-
- public void onClick(View arg0) {
-
-
- Toast toast=Toast.makeText(MainActivity.this, "Hello C# Corner !!", Toast.LENGTH_SHORT);
- toast.setGravity(Gravity.CENTER, 0, 0);
- toast.show();
-
- }
- });
-
- }
- }
- </code>
And, finally the output will be like:
Conclusion
If you encounter any problem with the code then just follow through the enclosed Eclipse-project file.