Sunday 3 February 2013

Simple Toast Example in Android



activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >



    <Button

        android:id="@+id/toast_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="Click Here to Display Toast" />



</RelativeLayout>


MainActivity.java


package com.rajeshvijayakumar.toast_example;



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 MainActivity extends Activity implements OnClickListener {



    private Button mToastButton;

   

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mToastButton = (Button) findViewById(R.id.toast_button);

        mToastButton.setOnClickListener(this);

       

    }



    @Override

    public void onClick(View v) {



        if(v.getId() == R.id.toast_button) {

            Toast.makeText(this, "You have clicked, So Toast is appeared", Toast.LENGTH_LONG).show();

        }

    }

}

Output :













1 comment: