Tima
Tima

Reputation: 12925

Override onClick color / drawable

I'm trying to find out, how to override standard onClick color (yellow) for Button with orange color?! (during writing this question I've seen, that these are no colors, but images)

Is there the easy way to do that? Or should I write a new style in that case?

I found in GIT, how com.android.internal.R.attr.buttonStyle looks like. And I would like just copy and modify that button style a bit. But if I do it on that way, I get XML-Errors

<?xml version="1.0" encoding="utf-8"?>
<selector>
    <item 
        android:state_window_focused="false" 
        android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal"/>
    <item 
        android:state_window_focused="false" 
        android:state_enabled="false"
        android:drawable="@drawable/btn_default_normal_disable" />
    <!-- Modified item -->
    <item 
        android:state_pressed="true" 
        android:drawable="@drawable/btn_default_selected" />
    <item 
        android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_default_selected" />
    <item 
        android:state_enabled="true" 
        android:drawable="@drawable/btn_default_normal" />
    <item 
        android:state_focused="true"
        android:drawable="@drawable/btn_default_normal_disable_focused" />
    <item 
        android:drawable="@drawable/btn_default_normal_disable" />
</selector>

Ok, I guess, I must fill selector's attribute xmlns:android. If I fill it with "http://schemas.android.com/apk/res/android", I get other errors. This time, because the android's drawable resources cann't be found.

Any suggestions?!

Thank you,

Mur

Upvotes: 1

Views: 3461

Answers (2)

Kevin Coppock
Kevin Coppock

Reputation: 134714

Mur, did you copy these resources (e.g. btn_default_normal) into your drawables folder? You'll have to get these resources (they can be found in your sdk folder under platforms/android-8/data/res/ then the different drawable folders) and then modify them to be the style that you need for each selector state.

Upvotes: 1

Zelimir
Zelimir

Reputation: 11038

One working example. This xml is set as background drawable. Adjust the colors as you like.

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

    <item android:state_pressed="true" > 
        <shape> 
            <gradient 
                android:startColor="#FFAAAAAA"
                android:endColor="#FFAAAAAA"
                android:angle = "180" />
            <corners 
                android:radius="10dip" /> 
        </shape> 
    </item> 

    <item android:state_focused="true" > 
        <shape> 
            <gradient 
                android:startColor="#FF888888"
                android:endColor = "#FF888888"
                android:angle = "180"/> 
            <corners 
                android:radius="10dip" /> 
        </shape> 
    </item> 

    <item>         
        <shape>
            <gradient 
                android:startColor="#FFFFFFFF"
                android:endColor = "#FFFFFFFF"
                android:angle = "180" />
            <corners 
                android:radius="10dip" />
         </shape> 
    </item> 
</selector> 

Additionally, you can define Stroke (button border).

Upvotes: 4

Related Questions