Monday, 1 July 2013

Android Toast Message Notification

Android Toast notification is a message that pops up on the surface of the window. It only fills the amount of space required for the message,the user activity remains visible and interactive . The notification remains on the screen for sometimes and fades away.

To display a simple Toast,we need to makeText for that Toast and then call show() to display it.
The code used for Toast notification is :

Toast.makeText(getApplicationContext(), "Toast Message", Toast.LENGTH_LONG).show();

Here makeText has three parameters :

1. Context - The application Context.
2. Text- The Text message that need to be Displayed.
3. Duration - The Duration of the Toast to be Displayed.

Here is the full code for Toast message :

Application Name : SimpleToast
Project Name : SimpleToast

Activity Name : MainActivity.java
Layout Name : activity_main.xml

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="178dp"
        android:layout_height="wrap_content"
        android:text="Toast Button" />

</LinearLayout>

MainActivity.java 

package com.example.simpletoast;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "This is a Toast Message", Toast.LENGTH_LONG).show();
}
});
    }


   
    
}

The Output of the program will be like this :


Android Toast example, Android Toast Demo, Android Tutorial Video

1 comments: