dcsimg

Wiring Up Android Buttons

Three ways to configure click handlers in Android UI Code

Quick tests

Sometimes a project demands that we code in a thorough, “belt and suspenders” fashion.

However many times it just isn’t necessary to go through all of the trouble — particularly when you’re trying out a new API for the first time and you really don’t need to build a production-ready project.

I find myself often throwing together a quick app to test something — in most cases I create a single button to initiate the test.

This article takes a look at three ways to accomplish this task. Pick the one which is right for you and the project you are working on.

Generally speaking an Android application’s UI is defined within an XML layout file. When you create a new project in Eclipse, the Android Developer Tools provide a layout (main.xml) that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

Now, let’s add a Button widget to the mix,named “Hit Me”.

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Hit Me"
	andorid:id="@+id/btnHitMe"
	/>

This Button needs to trigger an action in our code, so let’s have a look at the Java code behind this user interface.

Note that in order to work with a Button in code we need to import android.widget.Button.

import android.widget.Button;

And to wire up the handlers we define an instance of the android.widget.Button at the class level:

private Button btnHitMe = null;

Now, let’s get a reference to the widget within the onCreate method of our Activity class:

    btnHitMe = (Button) findViewById(R.id.btnHitMe);

The R.id.btnHitMe enumeration is automatically generated by the Android Developer Tools when the main.xml file is modified and saved. When the optional android:id attribute is included in the definition of the widget as shown in the earlier listing, the ADT automatically creates the enumerations to identify this widget throughout the project. These values are stored in R.java. Never modify this file by hand — it is a fool’s errand as it is constantly being re-written by the tools.

Once we have a valid reference to the widget from findViewById, we need to set up the click handler to process Button interaction:

        bHitMe.setOnClickListener(new Button.OnClickListener(){

        	public void onClick(View v){
                // process the button tap
        	}
        });

Within this anonymous class we implement whatever code is relevant for our test.

Here is the complete code for this anonymous handler approach, choosing to display a Toast notification when the Button is tapped, or “clicked”.

package com.msi.linuxmagazine.hotwiregui;

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

public class HotWireGui extends Activity {

	private Button btnHitMe;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnHitMe = (Button) findViewById(R.id.btnHitMe);
        btnHitMe.setOnClickListener(new Button.OnClickListener(){

        	public void onClick(View v){
                // process the button tap
        		(Toast.makeText(HotWireGui.this,"Clicked Me!",Toast.LENGTH_LONG)).show();
        	}
        });
     }
}

This code works fine though some Java folks might argue that anonymous classes are evil or some such other religious view about this code. Perhaps it is a bad habit, but not a topic for today’s discussion.

Evil or not, I prefer to not write all of this code so I tend to “cut-n-paste” this code from one project to the next. Let’s look at another approach — having the class itself be the “listener”.

Class level listener

The next approach to look at is where we have the Activity class implement the OnClickListener rather than employing an anonymous class.

To do this we need to import the Interface:

import android.view.View.OnClickListener;

Then we indicate that the class implements the Interface:

public class HotWireGui extends Activity implements OnClickListener

Of course, then we must actually implement this interface which means that we need a method named onClick which takes a single View argument. The full code is shown here:

package com.msi.linuxmagazine.hotwiregui;

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

public class HotWireGui extends Activity implements OnClickListener{

	private Button btnHitMe;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnHitMe = (Button) findViewById(R.id.btnHitMe);
        btnHitMe.setOnClickListener(this);
     }

	 public void onClick (View v) {
         // process the button tap
		 if (v.getId() == R.id.btnHitMe) {
			 (Toast.makeText(HotWireGui.this,"Clicked Me in onClick method!",Toast.LENGTH_LONG)).show();
		 }
	 }
}

Things to note about this code:

  • We still have to define the Button and get a reference to it with the findViewById() method.
  • We still need to setup the “OnClickListener” — though this time we simply pass in this.
  • The OnClick method can either make an assumption about the View it is receiving if there is only one we are working with, or we need to do some sort of check on the passed-in View instance to determine which code to run. This is of course only relevant if the Activity has to respond to more than one view that can raise the onClick event.

From a read-ability perspective, this approach may be easier as we’re not dealing with the nameless anonymous class. When there are many Buttons in play a bunch of calls to findByView followed by setOnClickListeners and the accompanying enclosed methods can be a bit tedious to sift through in the code — let alone write in the first place. With this single click handler and comparisons the code can be a bit easier to read and maintain. One downside however is that this one method is likely going to handle ALL clicks in the Activity.

OK, so let’s say you’re super lazy and you just don’t even want to do this modest amount of coding?

What can we do to wire up a Button without:

  • Implementing the OnClickListener interface, or
  • Without importing the Button class, or
  • Without finding the view and assigning the listener for each button in Java code?

Can this really be done? Yes — let’s look at a really quick and dirty way to implement a click handler for an Android Button to help you build simple apps without the fuss of all of that boiler-plate code.

Cheating?

We start by looking at the code — there’s just not much to it so we’ll just walk through it quickly.

We start out with the Button definintion in the layout file — we need to make a small change there. Don’t worry, it’s worth it.


<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hit Me”
android:id=”@+id/btnHitMe”
android:onClick=”HandleButton”
/>

This looks identical to the version we saw earlier with the exception that there is a single addition of a new attribute, namely android:onClick.

The value assigned to this attribute, HandleButton, refers to a developer-supplied method with the “onClick” signature of (View v).

Let’s have a look at the Java code implementing this Activity.

package com.msi.linuxmagazine.hotwiregui;

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

public class HotWireGui extends Activity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
	}

	public void HandleButton(View v) {
        if (v.getId() == R.id.btnHitMe) {
			(Toast.makeText(HotWireGui.this,"Hot wired Button!",Toast.LENGTH_LONG)).show();
		}
	}
}

Note that the Button class is not imported. There is no definition of a Button instance. No calls to findViewById. No calls to setOnClickListener. No extra sets of curly braces to match up on the anonymous class’ onClick handler, etc.

We must however provide a method with the correct signature. In this case we’ve called it HandleButton.

In the code we evaluate the passed-in View instance and check it’s Id. If it matches the one we’re looking for we implement our code. This same routine can handle an arbitrary number of Button’s on the screen.

The other neat thing here is that we can have an arbitrary number of these methods, named however we like, to further segregate the code for the Buttons. Want a different click handler name for a set of Buttons? No problem, just reference it appropriately in the XML layout file for the android:onClick attribute.

I stumbled upon this approach when writing a remote control for a robot with a bunch of buttons. Coding all of that boiler-plate code just bothered me and I was pleased to settle upon this approach.

Is it right for you? Perhaps. Let me know what you think.

Comments on "Wiring Up Android Buttons"

you have a great blog here! would you like to make some invite posts on my blog?

Every when inside a although we opt for blogs that we read. Listed below would be the most recent web pages that we pick out.

Usually posts some pretty interesting stuff like this. If you?re new to this site.

Im obliged for the blog post.Thanks Again. Much obliged.

Very few websites that come about to be in depth below, from our point of view are undoubtedly properly really worth checking out.

We came across a cool web-site that you simply may possibly get pleasure from. Take a search in case you want.

Here are some hyperlinks to web sites that we link to due to the fact we think they are worth visiting.

We like to honor several other web sites around the net, even if they aren?t linked to us, by linking to them. Below are some webpages worth checking out.

That may be the finish of this write-up. Here you will come across some web-sites that we think you?ll appreciate, just click the hyperlinks.

Wonderful story, reckoned we could combine a number of unrelated information, nonetheless truly really worth taking a search, whoa did a single discover about Mid East has got additional problerms at the same time.

Wonderful story, reckoned we could combine some unrelated information, nonetheless genuinely worth taking a search, whoa did one learn about Mid East has got extra problerms too.

Sites of interest we have a link to.

Below you will find the link to some sites that we think you ought to visit.

Although web-sites we backlink to below are considerably not connected to ours, we really feel they may be in fact worth a go through, so possess a look.

Below you?ll find the link to some internet sites that we assume you should visit.

Wonderful story, reckoned we could combine a handful of unrelated data, nonetheless truly worth taking a appear, whoa did a single learn about Mid East has got much more problerms also.

Here is a superb Blog You might Uncover Fascinating that we encourage you to visit.

Usually posts some really interesting stuff like this. If you are new to this site.

Every when in a when we pick blogs that we read. Listed beneath would be the latest web pages that we pick.

Check below, are some absolutely unrelated internet websites to ours, even so, they may be most trustworthy sources that we use.

Please visit the web sites we adhere to, such as this one, because it represents our picks through the web.

The time to read or visit the material or internet sites we have linked to below.

Very couple of sites that transpire to become comprehensive beneath, from our point of view are undoubtedly effectively really worth checking out.

That could be the finish of this write-up. Right here you?ll obtain some web-sites that we assume you?ll value, just click the hyperlinks.

We came across a cool web site which you may possibly appreciate. Take a appear should you want.

Please take a look at the sites we adhere to, including this one particular, as it represents our picks in the web.

Wonderful story, reckoned we could combine a couple of unrelated data, nonetheless genuinely worth taking a search, whoa did a single study about Mid East has got a lot more problerms too.

Check below, are some absolutely unrelated web-sites to ours, even so, they’re most trustworthy sources that we use.

The other day, while I was at work, my cousin stole my iphone and tested to see if
it can survive a 30 foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has 83 views.

I know this is completely off topic but I had to share it with someone!

Here is my weblog GastonYLopaz

I just want to tell you that I’m all new to weblog and truly liked your web site. Likely I’m want to bookmark your blog . You surely have good stories. Kudos for revealing your website.

Although web sites we backlink to beneath are considerably not connected to ours, we feel they’re truly really worth a go by way of, so possess a look.

Check below, are some completely unrelated web sites to ours, nonetheless, they’re most trustworthy sources that we use.

Very couple of internet sites that happen to be in depth below, from our point of view are undoubtedly nicely worth checking out.

Below you will uncover the link to some websites that we believe you must visit.

One of our guests just lately recommended the following website.

Here is a superb Blog You might Discover Fascinating that we encourage you to visit.

Always a large fan of linking to bloggers that I adore but don?t get a great deal of link like from.

The time to read or go to the content material or websites we’ve linked to beneath.

Check below, are some entirely unrelated sites to ours, on the other hand, they are most trustworthy sources that we use.

Here is a great Weblog You might Locate Intriguing that we encourage you to visit.

The details talked about in the article are a number of the best readily available.

Check beneath, are some entirely unrelated internet sites to ours, nevertheless, they are most trustworthy sources that we use.

Very few internet sites that happen to be in depth below, from our point of view are undoubtedly nicely worth checking out.

Check beneath, are some totally unrelated internet websites to ours, on the other hand, they may be most trustworthy sources that we use.

Very few websites that happen to become comprehensive below, from our point of view are undoubtedly properly worth checking out.

We prefer to honor a lot of other web sites on the net, even when they aren?t linked to us, by linking to them. Beneath are some webpages worth checking out.

We came across a cool site which you may well delight in. Take a look when you want.

Very few websites that occur to become comprehensive below, from our point of view are undoubtedly nicely really worth checking out.

Here are some hyperlinks to web sites that we link to simply because we think they may be really worth visiting.

We came across a cool internet site that you may appreciate. Take a appear in case you want.

Leave a Reply