5

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!

3 Answers 3

3

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.

4
  • I 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. Sep 10, 2010 at 16:51
  • Since 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.
    – softarn
    Sep 10, 2010 at 17:25
  • ok.. gotcha.. so I just need to know how to access the drawable that contains the default select/highlight colors.... Sep 10, 2010 at 17:37
  • You can find one xml and one java example in the link I posted.
    – softarn
    Sep 10, 2010 at 17:43
3

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.

0

Selectors are what you're looking for. Google around for tutorials. Here's one.

I Suppose you can find the Default Selector in the Android Source Code.

1
  • 10
    Is there any way to grab the default selector color programmatically? Sep 10, 2010 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.