Wednesday 24 December 2014

Message box on Button Click in Android (TOAST)

This tutorial explains how to show a message box on button click of an Android application.

Create a new Android project:

Create a new project as in the Hello World Android tutorial.

Application Name:  Message Alert
Project Name: MessageAlert
Package Name: com.skk.messagealert

Minimum SDK:  API 8
Target SDK: API 20
Compile With: API 20
Theme: Holo Light with Dark Action Bar

Activity Name: Message_Alert

1)      Remove the existing TextView “HelloWorld”

2)      Drag and drop a button from the Forms Widget Pallet to Android Screen and rename the Text properties to “Show Alert”





3)      Response for the button click in Android can be done via any of the two following ways,

a.       By setting onClick event:
In the properties of the button change the onClick event value to sendMessage. This will update the XML as below

android:onClick="sendMessage"

 Complete XML File:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.skk.messagealert.MessageAlert" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="98dp"
        android:layout_marginTop="46dp"
        android:onClick="sendMessage"
        android:text="Show Alert" />

</RelativeLayout>

Now we need to write the code for showing the Message on button click i.e the event, go to the java file MessageAlert.java under src\com.skk.messageAlert\ add the following method in MessageAlert class

public void sendMessage(View view) {
              Toast.makeText(getApplicationContext(), "You have clicked me!", Toast.LENGTH_LONG).show();
       }

Typically in this method we set the text that needs to be displayed when button is clicked, and how long it should be shown and show the message to the user.

a.       Using onClickListener event:
In this method we set the Listener event for the button. The below is the code for the same.

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_message_alert);
             
              Button bt1 = (Button) findViewById(R.id.button1);
              bt1.setOnClickListener(new View.OnClickListener() {
                    
                     @Override
                     public void onClick(View v) {
                           Toast.makeText(getApplicationContext(), "You have clicked me!", Toast.LENGTH_LONG).show();
                     }
              });
       }


Here we create a local variable to refer the button and add the EventHandler for that and on click of that we show the message using Toast.

4)      1)      After creating the code as in any one of the methods above, save and run the code as Android Application Project. This will launch the android application. Once you click the button you should be able to see the message being show.



Successfully we have created and deployed the sample message box in Android.

Additional Details:

Toast: Toast is used to show a small popup upon a specific action in Android, while the current activity remains visible and interactable.
makeText(): This is used to create the text message that needs to be displayed , duration and the application context.
show(): This method is called to displayed the created text message.
R.java:  This is an auto generated file for all the Android project, it creates a unique ID for all the elements in the Android application i.e Button, title, logo etc. Sample value below.

public static final int button1=0x7f05003c;


Note: The value of the button might differ for you.

Please provide your comments below the blog.

No comments: