views:

35

answers:

2

Hello all,

I am trying to create a button that has a custom background in the default state, but that still uses the colors for the pressed/selected states that are particular to whatever device it is running on.

From looking at the EditText source code, it appears that there is no such API, and that the pressed/selected colors are simply hard-coded:

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);
}

Can anyone verify that it is not possible for an app to know what the pressed/selected colors are on the current device?

If there is no way for an app to figure out what the pressed/selected colors are, then there is no way to create a Selector for a button that will match the UI across different devices.

It seems that Android must provide a way to find out what these colors are so that apps can use colors that are consistent with the UI of the device they happen to be running on.

Thanks!

-Tom B.

Follow-up, in response to CaseyB's comment, I tried the following:

StateListDrawable d = new StateListDrawable();
d.addState(android.R.attr.state_enabled, getResources().getDrawable(R.drawable.my_custom_background));
myButton.setBackgroundDrawable(d);

but it causes the button to use the custom background for all states. There doesn't appear to be a way to tell the StateListDrawable to use the default drawables for the pressed/selected states.

A: 

You should use a stateful drawable and just have it use the default drawable for those states.

CaseyB
Thanks for the reply, but creating a stateful drawable doesn't appear to automatically include the default drawable for the pressed/selected states. I updated my original post with the stateful drawable code that I tested.
tombz
A: 

The simplest approach is to create a custom style for your button inheriting from android:style/Widget.Button and adding android:background to your custom style.

Prashast
Thanks for the reply, but I am actually trying to set the background of the button in _only_ the default state. If you create a custom style and set the android:background property, then that background is used for all button states, including pressed and selected. I'm trying to set a custom background for only the default state, and preserve the default pressed/selected states' drawables.
tombz
I believe you can set different backgrounds for the different states of the button
Prashast