Issue
I want to support different themes in my android application. The themes should only differ in two colors. So there are these colors for every theme:
- Primary Color
- Secondary Color
What I want is, that there is one big default theme, that also has children like button styles et, like this (styles.xml):
<style name="DefaultTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="homeAsUpIndicator">@drawable/my_stackoverflow_arrow</item>
[...]
</style>
<style name="DefaultTheme.NoActionBar">
<!-- hide action bar and title -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="DefaultTheme.Button">
<item name="android:textAllCaps">true</item>
<item name="android:textStyle">bold</item>
[...]
</style>
But now I also want to change themes for different colors. Therefore, I have created a resource in the attrs.xml file:
<resources>
<attr name="ThemeColorPrimary" format="reference" />
<attr name="ThemeColorSecondary" format="reference" />
</resources>
So now I can use this attribute in my styles.xml from above like this:
<style name="DefaultTheme.Button">
<item name="android:textAllCaps">true</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">?attr/ThemeColorPrimary</item>
[...]
</style>
But how do I create my colored themes? I know I need to do something like this:
<item name="ThemeColorPrimary">@color/ThemeBluePrimary</item>
But where? And which theme inherits from which? If I now have something like:
<style name="BlueTheme" parent="DefaultTheme">
<item name="ThemeColorPrimary">@color/ThemeBluePrimary</item>
<item name="ThemeColorSecondary">@color/ThemeBlueSecondary</item>
</style>
<style name="GreyTheme" parent="DefaultTheme">
<item name="ThemeColorPrimary">@color/ThemeGreyPrimary</item>
<item name="ThemeColorSecondary">@color/ThemeGreySecondary</item>
</style>
<style name="BlackTheme" parent="DefaultTheme">
<item name="ThemeColorPrimary">@color/ThemeBlackPrimary</item>
<item name="ThemeColorSecondary">@color/ThemeBlackSecondary</item>
</style>
I can't specify in AndroidManifest.xml the theme like this:
<activity
android:name="StackOverflowActivity"
android:screenOrientation="portrait"
android:theme="@style/DefaultTheme.NoActionBar" />
Because the specified resources would be missing. Otherwise I also can't to this:
<activity
android:name="StackOverflowActivity"
android:screenOrientation="portrait"
android:theme="@style/BlueTheme.NoActionBar" />
because BlueTheme has no Child that is called "NoActionBar".
I know this is confusing, I am pretty confused myself. How can I solve this? Thank you in advance.
Solution
You have to specify those custom attributes in the parent theme:
<style name="DefaultTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="ThemeColorPrimary">@color/defaultPrimaryColor</item>
<item name="ThemeColorSecondary">@color/defaultSecondaryColor</item>
</style>
Then each children of DefaultTheme would have access to your custom attributes and also can override those.
If you want to combine NoActionBar and BlueTheme, then you have to subclass NoActionBar theme:
<style name="BlueNoActionBarTheme" parent="DefaultTheme.NoActionBar">
<item name="ThemeColorPrimary">@color/ThemeBluePrimary</item>
<item name="ThemeColorSecondary">@color/ThemeBlueSecondary</item>
</style>
Answered By - azizbekian
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.