Overriding styles in different values files - android

I'm using the new fontFamily attribute so that I can control the weight of font used. Because this was only introduced in API level 16 I've created a styles file in values-v16. It seems a bit silly to declare the colour, size etc in both values and values-v16 but it is necessary for all styling to be applied. Is there someway to declare font family in the values-v16 styles file and inherit the colour, size etc from the values version of the file.

Use a style inheritance. You can put all of the common (non-API specific) styles into a parent and inherit this in each of the children.
http://developer.android.com/guide/topics/ui/themes.html
<style name="v16" parent="CommonStyle">
Common file:
<style name="CommonStyle" >

Related

How to change theme attribute in code?

Is there anyway to change theme attributes dynamically? For example,set the "android:colorPrimary" attribute for a Theme. I know I can apply different Themes to Activity when it creates, but it requires predefined styles in resources, which means the attributes are fixed. If I want to download one Theme from server(it can be a config file with some color attribute defined), this can't work out.

How change theme color programmatically: style, attributes android?

I used google but didn't find an accurate solution, for changing themes programmatically.
I want to change everything programmatically: styles, colors, attributes etc., without using style.xml, theme.xml or any other xml file.
I have searched, but styles are defined in an xml file like that Using Themes in Android Applications
I know this is an old question, but if someone's still looking for a solution, checkout the GreenMatter library for the Material theme, or for the Holo theme holoaccent.

Managing multiple button selectors?

My calculator app consists of 30 buttons. I want to provide themes for the calculator keypad. A theme changes button background (gradients, not image backgrounds) and font. Some themes have the same color for all buttons while some have a color for numbers, another color for operators and so on.
The color change is using selectors from res/drawable/*.xml
How do I change the theme via the code?
Hopefully avoiding typing:
button.setBackground(Drawable background);
button.setTypeface(font);
30 times. And if I have 5 themes, then 30 * 5 * 2 = 300 lines of codes!!
I'm new to this and if there is no other way I'll go with the 150 lines.
Also how do I save the user theme selection? Using preferences?
You can create a custom XML theme which will change all of your XML components. After creating a new theme, go into the Android Manifest file and change the theme. For example:
<activity
android:name="com.myapp.MyActivity"
...
android:theme="#style/MyCustomTheme" />
To create the theme, go to res/values/themes.xml and create a new theme with an identifier:
<resources>
...
<style name="MyCustomTheme" parent="android:style/Theme">
<item name="android:textColorPrimary">#ffff0000</item>
</style>
...
</resources>
By using this method, you can create an extensive library of different themes and change to what theme you want.
NOTE: This is not just for changing the background, but it can also be used to change the theme of the buttons. Visit this website for more information:
http://janrain.com/blog/introduction-to-android-theme-customization/
EDIT: As that user commented, it is possible that you can put the function to change the theme of the button in a for() loop.
For your case, I have derived this from the link above. It will change the texture of the buttons in your XML file rather than in Java.
"Using a Custom Nine-Patch With Buttons
A nine-patch drawable is a special kind of image which can be scaled in width and height while maintaining its visual integrity. Nine-patches are the most common way to specify the appearance of Android buttons, though any drawable type can be used.
Example nine-patch PNG.
Notice the one pixel black lines around the edge, they control the scaling of the image.
Save this bitmap as MyApplication/res/drawable/my_nine_patch.9.png
Define a new style (you can define the new style in the same file that you defined your custom theme from Creating a Custom Android Theme above) …:
<resources>
...
<style name="MyCustomButton" parent="android:Widget.Button">
<item name="android:background">#drawable/my_nine_patch</item>
</style>
...
</resources>
Apply the new button style to the buttonStyle attribute of your custom theme:
<resources>
...
<style name="MyCustomTheme" parent=...>
...
<item name="android:buttonStyle">#style/MyCustomButton</item>
</style>
...
</resources>
Now the buttons in the activities your theme is applied to have custom images. However, you may notice that they don’t change appearance when selected. Read Selector Drawables below for an introduction to using multiple drawables to define one drawable that changes based on state."
From here, you can change certain components of the theme (such as the button texture as an image).
After you have a theme that looks good, apply it in the Android Manifest as I mentioned above.
I will FURTHER edit this if it still does not answer your question.

How can Theme.NoTitleBar inherit all attributes of Theme?

Theme.NoTitleBar is defined as below.
<style name="Theme.NoTitleBar"> // It does not have parent="Theme"
<item name="android:windowNoTitle">true</item>
</style>
But, it inherits all attributes of Theme.
I don't know how it works.
It inherits all attributes of Theme because it's basically an extension of that theme. It's somewhat similar to saying Class A extends B, although there are no distinctions in the visibility of attributes or anything.
Note how Theme.NoTitleBar is prefixed with Theme, that's where all the attributes come from.
Generally, the name of a style provides a hierarchical approach to structuring and naming styles and substyles. You can use the hierarchical characteristic as an alternative to the parent attribute for styles you've defined yourself. For extending existing platform-defined styles, you always need to use parent.
All this is explained pretty well in the Styles and Themes documentation, subsection Inheritance. Two relevant quotes that summarize most of above:
If you want to inherit from styles that you've defined yourself, you
do not have to use the parent attribute. Instead, just prefix the name
of the style you want to inherit to the name of your new style,
separated by a period.
Note: This technique for inheritance by chaining together names only
works for styles defined by your own resources. You can't inherit
Android built-in styles this way. To reference a built-in style, such
as TextAppearance, you must use the parent attribute.
Head over to the link for more details or to see some examples.

Android: HowTo make device theme

perhaps I don't know how to search for that - everything I found only is about changing the app theme.
I want to know how I can develop a whole theme for the android device itself?
On the market you'll find many themes out there - how can I build one, too?
I don't want to use 3rd party licences (ThemeMaker etc)...anyone who has a tutorial for me?
Thanks for your help!
You should read up on the Android Developer site - Styles and Themes http://developer.android.com/guide/topics/ui/themes.html
A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.
Styles in Android share a similar philosophy to cascading stylesheets in web design—they allow you to separate the design from the content.
Apply a theme to an Activity or application
To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name. For example:
<application android:theme="#style/CustomTheme">
If you like a theme, but want to tweak it, just add the theme as the parent of your custom theme. For example, you can modify the traditional light theme to use your own color like this:
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>

Categories

Resources