A Simple Toast Message in Android

June 11, 2012

A toast message is a message shown to the user. This message will be visible to the user for a  particular interval of time and will be disappeared automatically. In this application we will create an activity with a button called “Click Me”. Clicking on this button will display a Toast message on the screen.

1. Creating new project

New Android Project

Figure 1 : New Project

2. Select build target

Select API Level

Figure 2: Select target API level

3. Enter Application details :

Application Information

Figure 3: Application Information

4. Open the file /res/layout/values/strings.xml and add new string namely “lblBtnClickMe”. See the highlighted code given below:

<?xml version="1.0" encoding="utf-8"?>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">SimpleToastMessage</string>
    <string name="lblBtnClickMe">Click Me !!!</string>

5. Open the file /res/layout/main.xml and modify it with the given below :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >


6. Open /src/in.wptrafficanalyzer.simpletoast/MainActivity.java and update the file as given below :

package in.wptrafficanalyzer.simpletoast;

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 {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        // Getting an instance of the button btnClickMe defined in main.xml
        Button btn = (Button) findViewById(R.id.btnClickMe);

        // Defining onClickListener for the button btnClickMe
        OnClickListener listener = new OnClickListener() {

           // This will be executed when user touches the button btnClickMe
           public void onClick(View v) {
               // Display Toast Message "Hello Android"
               Toast.makeText(getBaseContext(), "Hello Android !!!" , Toast.LENGTH_SHORT ).show();

        // Setting onClick event listener for the button btnClickMe

7. Execute the application by clicking the menu Run -> Run of the eclipse

Application result

Figure 4 : Application in execution

8. Download

9. Reference


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner