A user-selectable theme (light and dark), with each theme having its own resources, is straightforward to implement if a single parent theme is used in the app:
<style name="MyApp.Dark" parent="android:Theme.Material">
<item name="themeBackground">#color/dark_grey</item>
<item name="themeBackgroundTransparent">#color/darkColorBackgroundTransparent</item>
<item name="colorPrimary">#color/darkColorPrimary</item>
.
.
</style>
<style name="MyApp.Light" parent="android:Theme.Material.Light">
<item name="themeBackground">#color/brown_50</item>
<item name="themeBackgroundTransparent">#color/lightColorBackgroundTransparent</item>
<item name="colorPrimary">#color/lightColorPrimary</item>
.
.
</style>
However, a typical Android app might employ multiple parent themes throughout the app such as the dialog theme, no action bar theme, or dialog-when-large theme. These themes would not be covered by current themes extending Theme.Material (because inheritance is expensive), so using these themes would necessitate additional style definitions for light and dark themes:
<style name="MyApp.DialogWhenLarge.Light" parent="android:Theme.Material.Light.DialogWhenLarge">
</style>
<style name="MyApp.DialogWhenLarge.Dark" parent="android:Theme.Material.DialogWhenLarge">
</style>
In order to apply themed resources to the new themes, the trivial solution is to copy and paste individual items from the MyApp.Dark theme into the new MyApp.DialogWhenLarge.Dark theme, et al, but the amount of duplicated code becomes unmanageable, incomprehensible, and error-prone with increasing numbers of themes and resources.
So my question is, what is a simple way to apply themed resources to multiple theme definitions?
Ideally, one would be able to apply a set of resources to a theme in the same way one would applies a style to a view, which would result in a solution of the following form:
<style name="MyApp.DialogWhenLarge.Light" parent="android:Theme.Material.Light.DialogWhenLarge">
<item name="themedResources">[pointer to canonical list of light theme resources]
</style>
Of course, it is also important that any solution retains the ability to reference themed resources from views in the format of ?themeBackground
Related
I'm trying to implement dark mode in my application. As far as I know, the best solution is to use Paris library. Is there any simple solution to set dark mode globally in the application?
I found issue on the Paris github: https://github.com/airbnb/paris/issues/15 but it is not implemented. Any idea is there another way to set style once and not using view.style(R.style.night_style) on each view?
You can use different themes like below example :
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight"></style>
Alternatively, if you want to define separate Light and Dark themes for your app, you can inherit from Theme.MaterialComponents.Light in the values directory, and Theme.MaterialComponents in the values-night directory. E.g.:
res/values/themes.xml
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
<!-- ... -->
</style>
res/values-night/themes.xml
<style name="Theme.MyApp" parent="Theme.MaterialComponents">
<!-- ... -->
</style>
The Theme.MaterialComponents theme is a static dark theme, whereas
Theme.MaterialComponents.DayNight is a more dynamic theme which will
help facilitate easy switching between your app’s Light and Dark
theme. If using a DayNight theme, you can define one app theme that
references color resources, which can be overridden in the
values-night directory if needed.
Reference
Starting to get my hands into that new update to the Google Support Library and I want to implement the Theme.AppCompat.DayNight into my app.
The problem I am having is that it seems no one explained how to customize it. So If I want to have a different colorAccent for day and a different one for night, how do I do that? Are you supposed to specify different dark and light themes to base off of? Thanks in advance!
You can use the night resource qualifier folder.
In this way you can define colors and the other resources for the dark (night) and for the light theme (day).
Qualifiers:
night: Night time
notnight: Day time
In order to support the dark theme with a Material Components Theme use:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
<!-- ... -->
</style>
With an AppCompat theme:
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<item name="colorPrimary">#color/primary</item>
</style>
Then define in your app theme the references color resources, and override the value in the values-night directory if needed:
Example: res\values\colors.xml:
<color name="colorPrimary">.....</color>
In res\values-night\colors.xml folders define the same color:
<color name="colorPrimary">.....</color>
I have an app with a few different Activities. The different activities have different styled buttons, texts, etc... I've set up all the components to have various styles based on their location/Activity. (Eg. style="#style/MainMenuActionBarTitle, or style="#style/MainMenuActionBarTagLine). These styles set the background (Drawable, MipMap, or Color), textColor, etc...
The app will be offering some theme packs which change the colors of these various components throughout the application, and I was hoping there was a way to have Styles with the same name, but different values based on the Theme of the app. This way I can just change the Theme whenever the Activity is loaded to whatever Theme the user has chosen.
There's some good info here on how to change the standard widget look & feel using Themes, but that changes the look and feel for the standard-un-styled widgets.
Is there a way to accomplish this using Themes, or is this the wrong direction altogether? Is there a better/easier route?
Edit: After doing more research and more fiddling, I've realized what I want to do isn't far off from how I can accomplish this. What I want to do is to actually change component Styles when I set the Theme of the Activity.
One solution I've discovered is to use attributes which the Theme can point to different Styles.
attrs.xml
<resources>
<!-- Attributes referencing whatever style the theme needs to set up. -->
<attr name="main_menu_button_style_play" format="reference" />
</resources>
themes.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- App specific attributes -->
<item name="#attr/main_menu_button_style_play">#style/MainMenu.Button.Play</item>
</style>
<!-- Blue application theme. -->
<style name="AppTheme.Blue" parent="AppTheme">
<!-- App specific attributes -->
<item name="#attr/main_menu_button_style_play">#style/MainMenu.Button.Play.Blue</item>
</style>
</resources>
styles.xml
<style name="MainMenu.Button.Play">
<item name="android:background">#f76d3c</item>
<item name="android:text">PLAY</item>
</style>
<style name="MainMenu.Button.Play.Blue">
<item name="android:background">#2ca8c2</item>
</style>
activity.xml
<Button android:id="#+id/main_play_button"
style="?attr/main_menu_button_style_play"/>
This works really well, and allows me to set the Theme in the Activity.onCreate() method.
The only annoying problem I have with this solution is that Android Studio complains that the Button is missing the layout_width and layout_height even though they're defined in the Style. I guess it doesn't follow the attribute reference back through the chosen Theme.
Another approach which is what I ended up using was to more heavily use the attributes. Creating attributes for all the properties values I want to change between themes. So, instead of main_menu_button_style_play which defines the style reference, I used main_menu_button_play_background. This approach is the same amount of work as simply specifying a style because themes can inherit, but the IDE understands it.
I am the using the ActionBarSherlock. I have the displayOption "homeAsUp" in my style.xml file. Now this shows a black arrow next to the title of the Activity. Since my theme is White on a blue blackground, i want to change the color of the black arrow, or maybe use a whole new icon resource in its place. How can i do this ?
Kind Regards.
Further to Eric's answer - I wasted a lot of time getting this right.
Remember that these items must go in the parent application theme, inheriting from Theme.Sherlock or similar.
<!-- CUSTOM APP THEME -->
<style name="AppTheme" parent="Theme.Sherlock">
<item name="android:actionBarStyle">#style/AppTheme.ActionBarStyle</item>
<item name="actionBarStyle">#style/AppTheme.ActionBarStyle</item>
<item name="android:homeAsUpIndicator">#drawable/action_bar_ic_ab_back_holo_dark</item>
<item name="homeAsUpIndicator">#drawable/action_bar_ic_ab_back_holo_dark</item>
</style>
Do not put them in the custom Action Bar theme inheriting from Widget.Sherlock.ActionBar.
<!-- ACTION BAR THEME -->
<style name="AppTheme.ActionBarStyle" parent="Widget.Sherlock.ActionBar">
<item name="android:icon">#drawable/action_bar_logo</item>
<item name="icon">#drawable/action_bar_logo</item>
<item name="android:displayOptions">showHome</item>
<item name="displayOptions">showHome</item>
</style>
Be careful when you style ActionBarSherlock !
Here is an extract from the web site (ActionBarSherlock Theming):
Due to limitations in Android's theming system any theme customizations must be declared in two attributes. The normal android-prefixed attributes apply the theme to the native action bar and the unprefixed attributes are for the custom implementation. Since both theming APIs are exactly the same you need only reference your customizations twice rather than having to implement them twice.
So in your case you MUST define two item:
<item name="android:homeAsUpIndicator">#drawable/icon</item>
and
<item name="homeAsUpIndicator">#drawable/icon</item>
Then you are sure that ALL your users will have the same L&F
I hope I can explain what I'm after. In essence, my users have asked me to allow different looks in my application, which I hope I can do with themes.
I hoped I could do something like this:
<style name="NewTheme" parent="android:Theme.Dark">
<item name="labelColor">#f90</item>
<item name="buttonColor">#fff</item>
<item name="buttonBg">#drawable/button</item>
</style>
<style name="OldTheme" parent="android:Theme.Dark">
<item name="labelColor">#fa0</item>
<item name="buttonColor">#88f</item>
<item name="buttonBg">#drawable/button_old</item>
</style>
And then reference these values in my styles.xml:
<style name="labelStyle">
<item name="android:textColor>#labelColor</item>
</style>
<style name="buttonStyle">
<item name="android:textcolor">#buttonColor</item>
<item name="android:background">#buttonBg</item>
</style>
I know this syntax is wrong, but what might be the right syntax? Basically, I want to create sets of attributes (color, background, a couple other things) and select them based on theme.
To work with themes and styles in Android you have to:
Define one or more themes in themes.xml and set the definitions of
your styles there.
Define custom attributes, a.k.a. custom styles, in attrs.xml.
Describe what the values of your custom styles are in styles.xml.
In your layout files, give your views a style attribute, which has a
custom style name as their values.
Set the theme of your application or activity in either
AndroidManifest.xml or in the Activity's onCreate(). This is done by calling setTheme() in the activity's onCreate() method, before any call to setContentView().
To change the theme, you simply need to restart your activity.
Iadvice you to look at this tutorial it deals with all that a programmer want to work on android themes (text color, text formatting, state list drawable etc ...)