views:

141

answers:

2

I created button with some shadow using a style:

<style name="test">
  <item name="android:shadowColor">#FFFFFF</item>
  <item name="android:shadowRadius">1</item>
  <item name="android:shadowDx">1</item>
  <item name="android:shadowDy">1</item>
</style>

This applies a white shadow on the button's text in its normal state. I was just wondering if anyone knows if there is a way to remove this shadow when the button is a pressed state. In other words, is there a way to apply another style when the button is in another (pressed) state?

Thanks in advance!

edit

bold.xml:

<resources>
    <style name="bold_text">
        <item name="android:textStyle">bold</item>
    </style>
</resources>

button.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item android:drawable="@drawable/test_pressed"
              style="@style/bold_text"
          android:state_pressed="true"/>
    <item android:drawable="@drawable/test_focused"
          android:state_focused="true"/>
    <item android:drawable="@drawable/test_normal"/>
</selector>

my layout:

<Button
        ...
        android:background="@drawable/button"/>
+1  A: 

You want a ColorStateList http://developer.android.com/guide/topics/resources/color-list-resource.html

schwiz
This method doesn't work... the ColorStateList only allows me to change the color of the text in different states. I was wanting to change the style (for example, increase text size when pressed or remove shadow when pressed) for different states.
This can do more than just colors, you can do whole drawables defined in xml, you just apply the style to your drawable.
schwiz
Hmm. I can't seem to get it to work. I created a new style called "bold" and created another XML called "button_test.xml" file containing a selector with the various states (pressed, focused, normal). I am just not sure how to reference "button_test.xml" in my layout.
you apply the styles to the drawables themselves in their xml file
schwiz
I'm not sure if I am completely understanding what you mean... I tried this previously by creating an XML file containing a <selector> with the various states and each state contains a different drawable and style. I reference that XML file I created in my layout using android:background="NEW_XML_FILE_I_CREATED." It doesn't seem to work.
It should work post one of your drawables and your selector xml
schwiz
I've edited the original post above. This is similar to what I have. I think the mistake lies in how I reference button.xml in my layout.
it should be android:background="@drawable/button" the button.xml file also needs to be in your drawable folder
schwiz
Oops, sorry, that was a typo. Yes, I do reference it via @drawable/button.
everything looks right from what you have posted at least. What exactly is happening, does it work w/o the style?
schwiz
Yes, the background of the button changes in the various states (without the style). With the style, the background still changes, but the text is not bolded when the button is pressed, which leads to my original question.
I think it has to do with the way I am referencing button.xml in my layout. I don't think it is correct to use android:background to apply a style onto the text.
I see, you actually need a separate selector for the text, it works exactly the same way as the drawable. Only you do it in android:text field instead of background. Sorry for the mix up.
schwiz
Well, I still can't seem to get it to work. It won't let me reference the selector I created (for the text) via android:text.
A: 

After a review of one of my button.xml files, I found that the order of evaluation (which is noted in the documents..) was such that my testing chose the same drawable. Correcting the order made it work.

Except not in the case of button2.xml. I made it work by removing all except the "drawable" and the "state_*" specifications. I had a "textColor" in there - looks like XML that gets past other checks (as does style=".." above) silently invalidates the selector. A good place to extend validation!

Schwiz has it right... for text use a separate selector defined in the "color" subdirectory of your resource directory: see ColorStateList.

DJC