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

  • SEARCH
  • Home
  • 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 9239283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:57:06+00:00 2026-06-18T07:57:06+00:00

I am trying to create a button that has a default image, but when

  • 0

I am trying to create a button that has a default image, but when it is pressed it switches to a new image. I have used this tutorial along with this question but it will not work.

This is my info_button_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- focused -->
    <item android:drawable="@drawable/ic_menu_info_details" /> <!-- default -->
</selector>

This is the code with my ImageButton:

                <ImageButton
                    android:id="@+id/apModeInfoButton"
                    android:layout_width="0dip"
                    android:background="@null"
                    android:layout_height="match_parent"
                    android:layout_weight="1.15"
                    android:clickable="true"
                    android:src="@drawable/info_button_selector" />

The button stays at the initial state looking like ic_menu_info_details the entire time.

I changed my info_button_selector.xml file to this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- focused -->
    <item android:drawable="@drawable/ic_menu_info_details2" /> <!-- default -->
</selector>

The ic_menu_info_details2 image was the displayed the entire time, which is expected. This shows that the button IS using the XML file as its drawable resource, but why is it that the button does not change its image when pressed?

EDIT

Using Joss’s answer below, the image still does not change, and is now stretched in a strange way. Note: Both images are exactly the same except the second image is scaled to be 60% the size. This might mean that it is working but I cannot tell as the view is stretched.

                <ImageButton
                    android:id="@+id/apModeInfoButton"
                    android:layout_width="0dip"
                    android:layout_height="match_parent"
                    android:layout_weight="1.15"
                    android:clickable="true"
                    android:background="@drawable/info_button_selector" />

I used both of these versions of the XML file as well as different combinations of the images, all of the combinations resulted in the same sized image never changing.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_menu_info_details" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/ic_menu_info_details" /> <!-- focused -->
    <item android:drawable="@drawable/ic_menu_info_details" /> <!-- default -->
</selector>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/ic_menu_info_details2" /> <!-- focused -->
    <item android:drawable="@drawable/ic_menu_info_details2" /> <!-- default -->
</selector>
  • 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-06-18T07:57:08+00:00Added an answer on June 18, 2026 at 7:57 am

    Set that drawable to the ImageButton background rather than the src.

     <ImageButton
              android:id="@+id/apModeInfoButton"
              android:layout_width="0dip"
              android:background="@drawable/info_button_selector"
              android:layout_height="match_parent"
              android:layout_weight="1.15"
              android:clickable="true" />
    

    Edit to follow up the distortion problem:

    Well what you can do is to have the layers separate.

    Because the background is stretchable, set this to the BACKGROUND and separate the image which shouldn’t stretch and set this to the SRC.

    <ImageButton
             android:id="@+id/apModeInfoButton"
             android:layout_width="0dip"
             android:src="@drawable/non_scaleable_image"
             android:background="@drawable/gradient_bg"
             android:layout_height="match_parent"
             android:layout_weight="1.15"
             android:clickable="true" />
    

    This way, you could have a background as a gradient and have it stretch without loosing quality, and the SRC image would not resize and distort.

    That is really what an ImageButton should be used for.

    Alternatively, do what I suggested in the first answer, but use a Button instead. Let me know if this helps.

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

Sidebar

Related Questions

I am trying to create a button that already has an index captures and
I am trying to create a delete button over each image that appears in
I'm trying to create a custom usercontrol that acts like a button, but i
I am trying to create a button that has a gradient and an icon
I'm trying to use javascript to create a button that has a onclick event
I'm trying to create a transparent (no button background) ImageButton that has a custom
I have a UIButton that has a default image, and another image for highlight/selected
I'm trying to create a button that fades out and then in when I
I am trying to create a button that can pop up the speech input
I am trying to create a flip button that shows changing content on every

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.