Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 755143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:05:44+00:00 2026-05-14T15:05:44+00:00

How can I have an imagebutton stay in the ‘pressed’ state when it is

  • 0

How can I have an imagebutton stay in the ‘pressed’ state when it is clicked? Basically I just want the background to be the depressed background, but I can’t figure out how to set it. At the moment, I’ve just copied the selected button background into my res folder, but when I set it as the background, it becomes blurry (since the original image is bigger than the button itself).

Normal Background:
alt text http://img707.imageshack.us/img707/9199/ss20100426163452.png
What I’m getting:
alt text http://img707.imageshack.us/img707/912/ss20100426163357.png
alt text http://img3.imageshack.us/img3/8304/ss20100426163623.png

Also I don’t believe I can actually use this method considering the many different UI layouts. The button should stay pressed as per the UI the user is using.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:05:45+00:00Added an answer on May 14, 2026 at 3:05 pm

    There are a few ways of doing this:

    First, you can simply use an ImageButton, and manually toggle its image drawable on click in Java. This is what the stock Music player on Android does for the shuffle button, for example. Although you won’t have control over the button background in its checked state, you’ll be able to swap out the image, which may be favorable from an Android UI-consistency perspective.

    Another option is to use a complex set of drawables and nine-patches to get an image inside a ToggleButton, with the option of changing the background and/or the image resource upon toggle. That’s the option I’ll show below. But remember, be cautious about UI consistency before doing this.


    res/layout/foo.xml

    ...
    <ToggleButton
        android:textOn="" android:textOff=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shuffle_button" />
    ...
    

    res/drawable/shuffle_button.xml

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- use "@android:drawable/btn_default" to keep consistent with system -->
        <item android:drawable="@drawable/toggle_button_background" />
        <item android:drawable="@drawable/shuffle_button_image" />
    </layer-list> 
    

    res/drawable/toggle_button_background.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- checked state -->
        <item android:state_pressed="false" android:state_checked="true"
              android:drawable="@drawable/btn_default_checked" />
    
        <item android:state_window_focused="false" android:state_enabled="true"
              android:drawable="@drawable/btn_default_normal" />
        <item android:state_window_focused="false" android:state_enabled="false"
              android:drawable="@drawable/btn_default_normal_disable" />
        <item android:state_pressed="true"
              android:drawable="@drawable/btn_default_pressed" />
        <item android:state_focused="true" android:state_enabled="true"
              android:drawable="@drawable/btn_default_selected" />
        <item android:state_enabled="true"
              android:drawable="@drawable/btn_default_normal" />
        <item android:state_focused="true"
              android:drawable="@drawable/btn_default_normal_disable_focused" />
        <item android:drawable="@drawable/btn_default_normal_disable" />
    </selector>
    

    res/drawable/shuffle_button_image.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/ic_mp_shuffle_on_btn" android:state_checked="true" />
        <item android:drawable="@drawable/ic_mp_shuffle_off_btn" />
    </selector>
    

    Image files

    • btn_default_<state>.9.png can be found in frameworks/base.git
      under
      core/res/res/drawable-hdpi
      and
      core/res/res/drawable-mdpi (also ldpi).

    WARNING: if you use these, your app will look inconsistent on devices with customized OS UIs (i.e. HTC’s Sense UI).

    • ic_mp_shuffle_<state>_btn.9.png need to be nine-patches, so that the image gets centered and not stretched to fit the button. Below are example hdpi versions of the icon:

    res/drawable-(h|m|ldpi)/ic_mp_shuffle_(on|off)_btn.9.png


    Final Note: Remember to be consistent with the system UI when possible, and be mindful of the fact that your app may run on devices with customized versions of the OS that have different graphics for UI elements like buttons. An example of this is HTC Sense, which has green buttons in place of the grey/orange/yellow ones in stock Android. So, if you end up copying the btn_default_... PNG files from the open source repository to create a toggle-able button background, you’ll break consistency on those devices.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ImageButton and a FileUpload controls in a child page of a
I have an ASP.NET web site which contains some ImageButton controls that cause postbacks
I'm trying to create a transparent (no button background) ImageButton that has a custom
I want to have a grid with 3 columns and 3 rows and an
I have a button which redirects the user to another page. Instead, I would
I'm using a ListView and have two buttons that allows users to delete or
Related to this question: On postback, how can I check which control cause postback
I have three images in my drawable folder, and an XML Image Button code:
I'm having trouble getting a td to have some text at its top and
how do i make my grdivew page more dynamic than hardcoded upto 5 pages?,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.