Wednesday 19 December 2012

Android Toast Tutorial

Android Toast Tutorial

In this tutorial,i am going to show how to create a simple toast display when a button is clicked.This tutorial is one of the simplest tutorial in the android.


Toast is used to show the notification or message when an action occur.


STEP BY STEP


1.Create a project 


2.In this tutorials when a button is clicked,a toast will show the message.
   Select the main.xml file from res/layout.

Copy and paste the bellow code.

<?xml version="1.0" encoding="utf-8"?>
<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/buttonToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Display Toast" />

</LinearLayout>


3.Select the main activity file
    Copy and pste the given bellow code.


package com.roney.toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ToastActivity extends Activity 
{
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) 
{
    Toast.makeText(getApplicationContext(),
                   "Button is clicked",            Toast.LENGTH_LONG).show();

}
});
}
}

4.Toast.makeText is used to declare the message to display


5.The output is given bellow





If you need to run this application in the android smartphone

0 comments: