Is there an Android API for clicking a default button or selected colors?

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

From looking at the source code of the EditText, it seems that there is no such API, and the pressed / selected colors are simply hardcoded:

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 someone make sure that the application cannot find out what types of colors are selected / selected on the current device?

If the application is not able to determine what pressed / selected colors are, then there is no way to create a selector for a button that will correspond to the user interface on different devices.

It seems that Android should provide a way to find out what these colors are, so that applications can use colors that match the user interface of the device on which they work.

Thanks!

-Tom B.

Subsequent observation, 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 this forces the button to use a custom background for all states. There seems to be no way to tell StateListDrawable to use the default drawings for clicked / selected states.

+4
source share
2 answers

The easiest way is to create your own style for your button, inheriting from android:style/Widget.Button and adding android:background to your custom style.

+1
source

You must use stateful inversion, and just use it by default for these states.

0
source

All Articles