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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:50:08+00:00 2026-06-14T17:50:08+00:00

I have a custom ImageButton class mimicing the ToggleButton’s checked state based on this

  • 0

I have a custom ImageButton class mimicing the ToggleButton's checked state based on this tutorial How to add a custom button state .

Everything works fine, when I have a state list drawable as the android:src attribute, but the custom state doesn’t work with the ImageButton's android:background attribute.

Here is my code:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.ImageButton;

public class CheckableImageButton extends ImageButton implements Checkable {

    private static final int[] STATE_CHECKED = {R.attr.state_checked};

    private boolean mChecked = false;

    public CheckableImageButton(Context context) {
        super(context);
    }

    public CheckableImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);

        if(mChecked){
            mergeDrawableStates(drawableState, STATE_CHECKED);
        }

        return drawableState;
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void setChecked(boolean checked) {
        mChecked = checked;
        refreshDrawableState();
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }
}

And the relevant snippet from the layout XML:

<com.my.package.view.CheckableImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@drawable/header_button_bg"
    android:padding="5dp"
    android:src="@drawable/menu_button"
    tools:ignore="ContentDescription" />

And the state list drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/my.package" >

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#ff000000" />

            <gradient android:angle="-90" android:endColor="#d2914e" android:startColor="#906434" />

            <corners android:radius="5dp" />
        </shape>
    </item>
    <item app:state_checked="true">
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#ff000000" />

            <gradient android:angle="-90" android:endColor="#d2914e" android:startColor="#906434" />

            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <stroke android:width="1dp" android:color="#ff000000" />

            <gradient android:angle="-90" android:endColor="#4f5b6c" android:startColor="#345b75" />

            <corners android:radius="5dp" />
        </shape>
    </item>

</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-14T17:50:09+00:00Added an answer on June 14, 2026 at 5:50 pm

    Another wonderful trait of eclipse probably..

    When I tried to revert my code to the last working version by hand (the state list drawable in the android:src tag), it produced the same error. I reverted from the SVN repo, it worked. Then I made the exact same changes as before, character by character, no difference, and voilá, it works now!

    So that’s it, the code in the question is fully functional.

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

Sidebar

Related Questions

I have a custom button class called ImageButton that extends JButton. In it i
I have created a custom control for ImageButton as <Style TargetType={x:Type Button}> <Setter Property=Template>
I have a Custom UITableViewCell class and the custom cell has an image button
I have ExpandableListActivity with custom CursorTreeAdapter . Everything worked fine until I added ImageButton
I have custom UITableViewCell . It contains UITextLabel . When I press this cell
I have custom validation rule: public function customRule($check) { } Inside this rule I
i have a listView with a custom BaseAdapter.Every list item has a clickable button.
I am creating a custom button to use in imageButton. I want to know
I have a list view with custom adapter, with an Image Button and a
I have made a custom button bar as described here . Now, I want

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.