11

I've created custom ToggleButtons in Android and since all buttons inherit from the same xml I want to change how they act depending on state, so when the state is checked I want to change the shadow color but this does not seem to possible with the current SDK.

I've created an xml file which holds button_colors:

<?xml version="1.0" encoding="utf-8"?>
<selector
   xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_checked="true"
    android:color="#FFFFFF"  />

<item
    android:color="#000000" />
 </selector>

But this only seems to work with text-color and not shadow color on the text. Is there something I'm missing? And rather not do this for every button manually in code since I want this to apply to every button in the app.

UPDATE EDIT:

My selector currently looks like this

<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:state_checked="true"
    android:drawable="@drawable/button_gradient_selected" />

<item
    android:drawable="@drawable/button_gradient" />

</selector>

But as I mentioned to the commentator below I can't seem to change the style/text-color-shadow from here since it only can take in a drawable it seems.

When I try to put in a different style on the button in here it force closes or either does not change the style depending on state. When I only try to put in the style here and have the drawable be set in the style it force closes. Either way it does not work it seems.

Joakim Engstrom
  • 6,243
  • 12
  • 48
  • 67
  • possible duplicate of [How to add shadow to TextView on selection/focus](http://stackoverflow.com/questions/4753158/how-to-add-shadow-to-textview-on-selection-focus) – Jeremy May 27 '12 at 16:27

4 Answers4

8

Seems that the Android framework does not support this.

From TextView.java:

        case com.android.internal.R.styleable.TextView_textColor:
            textColor = a.getColorStateList(attr);
            break;

        case com.android.internal.R.styleable.TextView_shadowColor:
            shadowcolor = a.getInt(attr, 0);
            break;

They treat textColor and shadowColor differently.

Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
3

Please refer to my solution on a different StackOverFlow question. I extended TextView to give a working solution here. (Replace TextView with Button)

Community
  • 1
  • 1
Gilbert
  • 868
  • 8
  • 14
-1

This is the Selector file you have to implement:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="false" android:drawable="@drawable/btn_toggle_off" /> 
  <item android:state_checked="true" android:drawable="@drawable/btn_toggle_on" /> 
  </selector>

These are the picture tey used for the default ToggleButton: btn_toggle_on and btn_toogle_off

Praveen
  • 90,477
  • 74
  • 177
  • 219
  • 1
    Thanks for your answer but this is not the answer since a drawable can not change shadow color only the background drawable. According http://developer.android.com/guide/topics/resources/color-list-resource.html you should use a separate selector for colors, but this does not seem to apply to shadow. I already have an XML that selects drawable but can not figure out how to change the shadowcolor or style from there. – Joakim Engstrom Feb 17 '11 at 09:01
  • @Joakim Engstrom: I think that i am not getting you properly. Can you post the screenshot of your activity. Then it will be undestandable for me. – Praveen Feb 17 '11 at 10:11
-3

You can have a selector for the shadow color like this: color_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true">
    <item 
        android:state_pressed="true"
        android:color="@color/btn_text_on" />
    <item 
        android:state_focused="true"
        android:color="@color/btn_text_on" />
    <item
        android:color="@color/btn_text_off" />        
</selector>

and then use this selector while styling your button in styles.xml like this:

<style name="ButtonStyle">
<item name="android:textColor">#FF383C48</item>
    <item name="android:textSize">12sp</item>
    <item name="android:shadowColor">@drawable/color_selector</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">1</item>
    <item name="android:typeface">sans</item>
    <item name="android:textStyle">bold</item>
</style>
Saurabh
  • 3
  • 1