Android Tutorial #4.1: Image & Text-Only Buttons (UPDATED)

This very simple tutorial will add to your Android UI (user interface) development arsenal. How? Buttons that display as simple text or as images are basic elements of any application. By following the two steps below, these buttons can be easily created with Google’s Android by simply extending the current Button and ImageButton classes.

We will create these two new Button extensions as shown:

Text and Image Buttons

Step 1: Set Background to ‘null’ in XML or code

The recommended technique is to use the ‘empty’ drawable variable in your ImageButton or TextButton’s XML to set your background to null:

android:background=”@android:drawable/empty”

An optional technique is to extend ImageButton or TextButton and to set the background to null in the constuctor. As above, this is not the recommended approach but may be useful at times:

public YourButtonExtension() {

setBackground(null);

}

And that’s it, only your image or text will display but will have button functionality…except for visually displaying focus. Let’s address that next.

Step 2: Handle onFocus() by overriding onDraw()

We listen for the focus event and update the Button’s text color or button image that way as well. But we’ll do it this way for now.

public void onDraw(Canvas canvas) {

// Since this Button now has no background. We must set the text color to indicate focus.
if (isFocused()) {

// Set the focused text color. In the case of ImageOnlyButton we would .
// instead do setImageResource(imageResourceFocused);
setTextColor(focusedTextColor);

} else {

// Set the non-focused text color. In the case of ImageOnlyButton we would .
// instead do setImageResource(imageResourceNotFocused);
setTextColor(notFocusedTextColor);

}

super.onDraw(canvas);

}

You might be wondering where those text color and image variables come from. We will obtain these focused & not-focused images or text colors from variables passed in from the XML resource file as you’ll see by reading the source code. I won’t go into how that happens here. Instead, I’ll keep this tutorial simple and point you to my next tutorial where I will discuss how to create & pass such custom variables.

And That’s It

You can download the source to see for yourself.

Prior Tutorials

Tutorial 1: Transparent Panel (Linear Layout) On MapView (Google Map)
Tutorial 2: “Hit” testing on a View (MapView)
Tutorial 3: Custom Media Streaming with MediaPlayer

Advertisement

15 Responses to “Android Tutorial #4.1: Image & Text-Only Buttons (UPDATED)”


  1. 1 Magellan August 29, 2008 at 6:30 am

    And for Pressed state ?

    Your button works fine for focused (with directionnal keypad, but is animated with a third state when we press the button with finger.

    Do you know the right way to do this ?

  2. 2 Biosopher August 29, 2008 at 7:03 am

    Hi Magellan ,

    Last time we tried using the old (Pre-0.9) SDK, there wasn’t a way to capture finger presses. We’ll update these tutorials as we migrate them to the new SDK though. Perhaps we’ll find a solution at that time.

    Sorry…

  3. 3 Arun October 31, 2008 at 5:39 am

    This is not working for me, in and Android 1.0, can you please tell how to do that.

  4. 4 Maxim Kornilov February 23, 2009 at 2:13 pm

    Hello,

    Thank a lot for this article.

    In your code you wrote:

    // Requesting focus doesn’t work for some reason. If you find a solution to setting
    // the focus, please let me know so I can update the tutorial
    requestFocus();

    I resolved problem with the requesting focus by adding the following line in init method:

    this.setFocusableInTouchMode(true);

    Also, method call requestFocus() can be safely removed.

    Maybe it will helpfull.

    Thanks.

  5. 6 Android Phone Tips April 18, 2011 at 4:56 pm

    I am still confused with the usage of javascript like the picture above, is there anything easier?
    but this can add to the experience for me, maybe good to try.

  6. 7 Jose Vigil October 28, 2011 at 10:49 am

    This worked for me. Thanks
    android:background=”@android:color/transparent”


  1. 1 Android Tutorial #4.2: Passing custom variables via XML resource files « Pocket Journey Trackback on May 2, 2008 at 12:28 pm
  2. 2 Android Tutorial 3: Custom Audio Streaming with MediaPlayer « Pocket Journey Trackback on June 4, 2009 at 7:14 am
  3. 3 Image & Text-Only Buttons (UPDATED) | Androidapps.org - Your source for technology news Trackback on December 10, 2009 at 10:03 am
  4. 4 Passing custom attributes via XML resource files | Androidapps.org - Your source for technology news Trackback on December 10, 2009 at 10:03 am
  5. 5 Tutorial 1: Transparent Panel (Linear Layout) On MapView (Google Map) « Pocket Journey Trackback on December 27, 2009 at 8:44 am
  6. 6 [SOLVDED] Android: Create custom buttons, Image & Text-Only « RussenReaktor’s Weblog Trackback on March 21, 2010 at 3:23 am
  7. 7 Android Tutorial #4.2: Passing custom attributes via XML resource files - Android Development Tutorial Trackback on October 1, 2010 at 6:33 pm
  8. 8 Passing custom attributes via XML resource files | Herwart's space Trackback on August 17, 2011 at 4:46 am

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




a


Follow

Get every new post delivered to your Inbox.