Android: Use default highlight colors in a custom button
Solution 1
You are asking for two different things, do you want the drawables or the colorcode?
Anyway, you can find the name of the drawables here: http://androiddrawableexplorer.appspot.com/ I don't know if you can use them directly from your app or if you have to save them to your drawables folder first, but you can find them in your sdk. If you want the colorcodes, use gimp to extract them from the pictures.
Solution 2
It seems that you can use a selector as drawable inside a selector!
(You can or should not use @android:drawable/btn_default_selected
, because it is private)
This meens that you can write your own selecter and use the whole default android selector for the items you want the default behavior for.
I used this selector
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@android:drawable/btn_default" android:state_pressed="true"/>
</selector>
And added it to as background to a linear layout. I don't know why, but this messed up the padding/margin as well, thats why i set them to 0.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_linear_layout_button"
android:padding="0dp"
android:layout_margin="0dp"
android:orientation="vertical" >
<!-- YOUR LAYOUT THAT ACTS LIKE A BUTTON -->
</LinearLayout>
The Result is that you have the parent background color in the unpressed state and the android background color for the pressed state.
Wise Shepherd
Updated on June 15, 2022Comments
-
Wise Shepherd almost 2 years
I want to use a Button in my android app but I want to customize how it looks. However, I want the highlight and selected colors of the button to be the same as the default colors (i.e. the dark and light orange gradients, or whatever the theme color supplies).
Is there anyway to get the default highlight/selected drawables and to use that as the fill for my buttons on the selected and highlighted states?
Thanks!
-
Wise Shepherd over 13 yearsI guess I want the color code, with which I can then create my own drawable? Basically I want to ensure that my highlight/selected color are always consistent on each device. So I don't want to hardcode in the orange color since this may change in the future and some devices use a different color. Rather I would like to grab the colorcode programmatically.
-
Wise Shepherd over 13 yearsIs there any way to grab the default selector color programmatically?
-
softarn over 13 yearsSince android is using drawables and not colorcodes for their buttons you wont be able to get the colorcode in any programmatically reasonable way. Well I'm sure that there is a way somehow but I don't think that it's worth it.
-
Wise Shepherd over 13 yearsok.. gotcha.. so I just need to know how to access the drawable that contains the default select/highlight colors....
-
softarn over 13 yearsYou can find one xml and one java example in the link I posted.