Sunday, 14 October 2012

Detecting and Handling Button Clicks on Android

In Android apps, there are two common ways to detect and handle user clicks on UI elements such as buttons: by attaching a listener to a layout item in Java or by including an onClick attribute in the item's layout XML code. Either way, you will typically include the Java code to execute on clicks inside the Activity class hosting the layout with the clickable item in it.

Here's an overview of each approach:

Attaching the Listener in Java

Include the button in an XML layout file, assigning it an ID attribute so that you can identify it in the Java Activity code:
<Button
android:id="@+id/your_button_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/your_button_label"
/>

The text attribute should be a string included in your res/values/strings.xml file with the specified name:
<string name="your_button_label">Click Me</string>

In the Activity using the layout, applied as follows...
setContentView(R.layout.layout_file_name);

...extend the class to implement the click listener interface:
public class YourActivity extends Activity implements OnClickListener

In your onCreate method, find a reference to the button and attach a click listener to it:
Button yourBtn = (Button)findViewById(R.id.your_button_id);
yourBtn.setOnClickListener(this);

Add the onClick method to the class:
public void onClick(View v) {

}

Inside the method, find out whether your button has been clicked:
if (v.getId()==R.id.your_button_id) {  

}   

Inside the conditional block, you can add whatever code you want to execute when users click the button.

Using the onClick Attribute

Using the onClick attribute is typically a little simpler, although there are some cases in which it isn't possible, for example where UI items are being created programmatically in Java rather than in XML layout files.

To use this approach, include your button in an XML layout file with one additional line:
<Button
android:id="@+id/your_button_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/your_button_label"
android:onClick="buttonClicked"
/>

The onClick method should include the name of the method you want to execute when users click the button. In the Activity class, the layout is set as the content view using the same technique as the first approach. When you use the onClick attribute there is no need to implement the click listener interface, to retrieve a reference to the button or to attach an event listener to it. Instead of providing the onClick method, include the method listed as your XML attribute in the Activity class hosting the layout with the button in it:
public void buttonClicked(View view){

}

The method name must match what you included in your XML attribute. Whatever method you list as an onClick attribute will receive a view parameter representing the UI item clicked. This allows you to use the same method to handle clicks on more than one item, just check which one has been clicked using the passed view parameter. Inside the method, include whatever code you want to execute when users click the button.

The onClick attribute is available on devices running Android API 4.0 and up.

No comments:

Post a Comment