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 3228004
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:38:27+00:00 2026-05-17T16:38:27+00:00

I am trying to remove an ImageButton’s background in only the default state. I’d

  • 0

I am trying to remove an ImageButton’s background in only the default state. I’d like the pressed and selected states to behave as usual so that they look correct on different devices, which use different colors for the pressed and selected states.

Is there any way to set an ImageButton’s background default state’s drawable without affecting the pressed and selected states?

I’ve tried to do this with a selector, but it does not appear to allow you to use the default drawables for some states – you have to set all the states yourself. Since there’s no API to retrieve the device’s default pressed/selected drawables, I don’t know what to set the pressed/selected states to.

I also tried getting the StateListDrawable object for the button that the system creates when you are not using a selector and then modify it change the default state. That didn’t work either.

I seems that on Android, if you want to change the drawable for one state of a button, then you must set all the states, and thus cannot preserve the default drawables for the other states. Is this correct?

Thanks!
-Tom B.

  • 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-17T16:38:28+00:00Added an answer on May 17, 2026 at 4:38 pm

    Tom,

    It’s true that if you override the default state you also have to override the pressed and focused states. The reason is that the default android drawable is a selector, and so overriding it with a static drawable means that you lose the state information for pressed and focused states as you only have one drawable specified for it. It’s super easy to implement a custom selector, though. Do something like this:

    <selector
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/custombutton">
    
        <item
            android:state_focused="true"
            android:drawable="@drawable/focused_button" />
        <item
            android:state_pressed="true"
            android:drawable="@drawable/pressed_button" />
        <item
            android:state_pressed="false"
            android:state_focused="false"
            android:drawable="@drawable/normal_button" />
    </selector>
    

    Put this in your drawables directory, and load it like a normal drawable for the background of the ImageButton. The hardest part for me is designing the actual images.

    Edit:

    Just did some digging into the source of EditText, and this is how they set the background drawable:

    public EditText(/*Context context, AttributeSet attrs, int defStyle*/) {
        super(/*context, attrs, defStyle*/);
    
                StateListDrawable mStateContainer = new StateListDrawable();
    
                ShapeDrawable pressedDrawable = new ShapeDrawable(new RoundRectShape(10,10));
                pressedDrawable.getPaint().setStyle(Paint.FILL);
                pressedDrawable.getPaint().setColor(0xEDEFF1);
    
    
                ShapeDrawable focusedDrawable = new ShapeDrawable(new RoundRectShape(10,10));
                focusedDrawable.getPaint().setStyle(Paint.FILL);
                focusedDrawable.getPaint().setColor(0x5A8AC1);
    
                ShapeDrawable defaultDrawable = new ShapeDrawable(new RoundRectShape(10,10));
                defaultDrawable.getPaint().setStyle(Paint.FILL);
                defaultDrawable.getPaint().setColor(Color.GRAY);
    
    
    
                mStateContainer.addState(View.PRESSED_STATE_SET, pressedDrawable);
                mStateContainer.addState(View.FOCUSED_STATE_SET, focusedDrawable);
                mStateContainer.addState(StateSet.WILD_CARD, defaultDrawable);
    
                this.setBackgroundDrawable(mStateContainer);
    }
    

    I’m sure you could adapt the idea to your purposes. Here is the page I found it on:

    http://www.google.com/codesearch/p?hl=en#ML2Ie1A679g/src/android/widget/EditText.java

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

Sidebar

Related Questions

I'm trying to remove scrollbar from my facebook app. I tried js solution, that
Hello I was trying to remove objects from object array that I have and
Trying to remove the last child of an element only if the class =
Trying to remove the full url that is being returned to imgurl: Usually returns
I'm trying to remove everything from a string that is NOT an html tag
Trying to remove a node by index now. I'd like to print out the
I am trying to remove the toolbar from Sencha Picker (I only need the
I am trying to remove any elements from an NSMutableArray object that contain a
I am trying to remove default applications from my rooted Galaxy S2 phone. I
i am trying to remove records of an employee that contains ?(null values). the

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.