Android button click

In this Android tip, I am going to show you how to handle button click. In Android there are two approaches to do actions when a button is clicked. For the first approach, you need to specify the method to be invoked when the button is clicked using the Android:onClick="method_name" in xml layout file. Then you will write the body of the method in the activity.
Alternatively, you can handle the button click in code using the setOnClickListener() method. You can pass to this method an instance of a class that implements the OnClickListener interface. You must override the onClick() method to do actions when the button is clicked.




Now to have a workable example application on handling button click, you create a new Android project. Then modify the activity_main.xml file to add two Buttons. The first button (Button 1) implements the first approach and the second button (Button 2) implements the second approach.

<LinearLayout 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=".MainActivity"
   android:orientation="vertical"
   >

   <Button
     android:id="@+id/bt1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Button1"
     android:onClick="buttonClick"
       />
  
   <Button
     android:id="@+id/bt2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Button2"
    
      />

</LinearLayout>


In the MainActivity class (in MainActivity.java file), you write code for the buttonClick() method. The buttonClick() method is invoked when the first button is clicked. For second button, you need a class called ClickReceiver that implements the OnClickListener interface. The onClick() is overridden to display a Toast message when the button is clicked.

 package com.example.buttonexample;

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

public class MainActivity extends Activity {

Activity context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
//create a reference to Button1
Button bt2=(Button)findViewById(R.id.bt2);
//receive click event
bt2.setOnClickListener(new ClickReceiver(){

});

}


private class ClickReceiver implements OnClickListener{
//show Toast message when the button is clicked
public void onClick(View view){
Toast.makeText(context,((Button)view).getText()+" clicked", Toast.LENGTH_SHORT).show();

}
}
public void buttonClick(View view){
//show Toast message when the button is clicked
Toast.makeText(this, ((Button)view).getText()+" clicked", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


Android-button-click

Posted by: Dara | post date: 09-10-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.