3

i am able to declare a theme and a specific button design:

<style name="MyTheme" parent="android:style/Theme.Black">
  <item name="android:buttonStyle">@style/Button.b1</item>
</style>

<style name="Button.b1" parent="android:style/Widget.Button">
    <item name="android:textColor">#000000</item>
</style>

The problem is that this style is applied to all buttons. I would like to declare a specific button with his own style changing each theme independent of the other buttons (something like a "save"-button). Any idea?


I tried the following:

<style name="Button.MyButton" parent="android:style/Widget.Button">
  <item name="android:background">@drawable/shape</item>
</style>

 <style name ="Button.MyButton.Theme1">
     <item name="android:textColor">#000000</item>
  </style>

 <style name ="Button.MyButton.Theme2">
     <item name="android:textColor">#FFFFFF</item>
  </style>

 <Button
     android:id="@+id/save_button" 
     android:layout_width="0px" 
     style="@style/Button.MyButton"
     android:layout_weight="1"
     android:layout_height="wrap_content"
     android:text="@string/save"/>

Now the button is independent from the theme, but it should also apply the style attribute that I declared above. At the moment it only applies the attributes that are declared in the MyButton scope and not these in MyButton.Theme1 or MyButton.Theme2.

Anthea
  • 3,741
  • 5
  • 40
  • 64

3 Answers3

3

If your theme MyTheme is used only to define the button style, remove it; also remove the parent property from the button:

<style name="Button.b1">
    <item name="android:textColor">#000000</item>
</style>

Then in the layout, use the style only for those button you need to, like below:

 <Button
         android:id="@+id/btn_custom"
         style="@style/Button.b1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" >
 </Button>
Adinia
  • 3,722
  • 5
  • 40
  • 58
  • Mytheme is larger then here, but it should contain a specific style element that contains information just for the save button. If I change the theme the save button should change also in his own way. – Anthea Apr 24 '12 at 10:11
  • This doesn't mean you have to set the 'Save' button style inside MyTheme. How are you changing the theme? programmatically? – Adinia Apr 24 '12 at 10:17
  • Since it should be at runtime I think i will do this at the beginning of every activity programmatically. – Anthea Apr 24 '12 at 10:18
  • Then you should probably also apply the 'Save" button special style at the same time. (that is, if you find a way to programmatically apply styles, maybe this (http://blog.infidian.com/2008/05/02/android-tutorial-42-passing-custom-variables-via-xml-resource-files/) is useful also) – Adinia Apr 24 '12 at 10:28
1

You can remove MyTheme declaration from your styles.xml and easily set @style/Button.b1 in the android:style property of your button of choice :)

edit: I think I finally got what you're asking for. I've no tried myself but it should work:

<!-- Declare your themes here -->
<style name="Theme1" parent="android:style/Theme.Black"></style>
<style name="Theme2" parent="android:style/Theme.Black"></style>

<!-- Declare your "abstract" Button style -->
<style name="MyButton" parent="android:style/Widget.Button">
  <item name="android:background">@drawable/shape</item>
</style>

<!-- Override MyButton style for Theme1 -->
<style name ="Theme1.MyButton" parent="@style/MyButton">
  <item name="android:textColor">#000000</item>
</style>

<!-- Override MyButton style for Theme2 -->
<style name ="Theme2.MyButton" parent="@style/MyButton">
  <item name="android:textColor">#FFFFFF</item>
</style>
a.bertucci
  • 12,142
  • 2
  • 31
  • 32
0

You should use your Button.b1 style directly only for those buttons you want to be styled that way. Use those buttons' android:style property:

android:style="Button.b1"
devmiles.com
  • 9,895
  • 5
  • 31
  • 47