30758

Android apply a theme on a custom item

Question:

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?

<hr />

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.

Answer1:

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>

Answer2:

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>

Answer3:

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"

Recommend

  • android resolution need help
  • Moving cursor at beginning UItextview [duplicate]
  • Spinner with border
  • Android, Custom Notification, Giving me always white Text
  • Is it good practice to put Edge Side Includes into my templates?
  • How can I sum two different columns at once where one contains Decimal objects in pandas?
  • Capturing HTML Text Input Key press after key has been applied?
  • Moving data between processes in Spartan 3
  • How to have a website splash screen (web app)
  • Generic classes with Collection getter of other types
  • Creating a Multi-Step Modal Using Jquery
  • How gzip file gets stored in HDFS
  • Primefaces lazy datascroller calling load twice
  • Two Tables Serving as one Model in Rails
  • How to access meteor package name inside package?
  • C#: Import/Export Settings into/from a File
  • Rest Services conventions
  • SonarQube: Cannot deactivate rule with missing quality profile
  • JqueryMobile Popup menu is not working
  • Security issues with PHP's Readfile method
  • How can I set a binding to a Combox in a UserControl?
  • Debug.DrawLine not showing in the GameView
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • AJAX Html Editor Extender upload image appearing blank
  • Magento Fatal error: Maximum execution error solution, on WAMP
  • Android full screen on only one activity?
  • C++ Partial template specialization - design simplification
  • Repeat a vertical line on every page in Report Builder / SSRS
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Read text file and split every line in MSBuild
  • C# - Serializing and deserializing static member
  • Java applet as stand-alone Windows application?
  • Join two tables and save into third-sql
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • How to model a transition system with SPIN
  • Adding custom controls to a full screen movie
  • ORA-29908: missing primary invocation for ancillary operator
  • Cannot Parse HTML Data Using Android / JSOUP
  • Error creating VM instance in Google Compute Engine
  • Converting MP3 duration time