Multi theme support in android app - android

Did anyone implemented multi theme support for android app? Are there any common practices for this?
Thanks for any advice.
UPD: The main problem for now is that android's theme engine doesn't support selectors like in CSS. For example if I have two views with "background" attribute there's no way to make theme engine distinguish those ones and set different backgrounds. I can specify different style for each view but this approach lacks flexibility 'cause it's impossible to apply style for whole activity at once.

As far as I know, there is no way to set a theme to the whole application in on line of code. If you want to change the theme of an activity, you need call setTheme() in its onCreate() method, BEFORE calling setContentView. So to make it easier for you, you could do a switch on all your themes, and select one in regards of what the user has selected. Now, if you want it to apply easily to all your activities, you could make all your activities be a subclass of a custom Activity in which you would only set the theme. Activity <-- ThemeActivity <-- all your Activities

Related

Android How to set windowActionModeOverlay using code at runtime

I have a android custom view that start a custom action view when long clicked, as such, it require the theme of the activity to have the style "windowActionModeOverlay" set to true. I don't want to define a custom theme. I want to use whatever theme the user provided for the activity, and set the style to true at runtime using code myself.
Is this possible ?
Please note any suggestion by changing style xml file is not what I am looking for, unless it allow user to use whatever theme they choose for their activity.
Thanks
There are tons of solution for changing the style of a single view. There are tons of solution for changing theme at runtime, but I only want to change a single style of a theme, and so far, I can not find a solution myself or online

How to apply different theming to the single activity approach

I'm trying out the single activity approach together with the Navigation Component but I was wondering how to apply multiple themes to one activity.
Normally I would set a theme to an activity via the AndroidManifest.xml and all the fragment within that activity will use that same styling. Whenever I want to use another theme I would simply create another activity and apply the other styling. But since there only is one activity I would have to change the styling after the activity has been created. This is fine for styling used by Fragments. However styles for the StatusBar and NavigationBar are a lot harder because they affect the Activity.
Things I've tried:
Change the theme of the Activity after is has been created. Unfortunately these changes aren't applied directly and would require a restart of the activity (which is the one currently running).
Set styles like StatusBar and NavigationBar when creating the Fragment. This works, but it requires me to set the themes and styles using code instead of using the usual themes.xml and styles.xml.
Can anybody tell me what the intended solution is? Because of these limitations we started adding activities again to the navigation component whenever the theme needed to be changed.

Change android theme runtime

I have backend response with some bunch of colors that I need to change in my app. Is there any way to change theme attributes for activity? I understand that I can change whole theme but I don't know what colors will be used in future. So my app has to be customizable totally from the backend
You can call setTheme() for an activity before calling setContentView(). The theme will be changed just for that activity.
If you want totally customized colors (that can't be captured in a set of pre-defined themes) then you're going to have to set up your activity and its views to do that in code.

Create Style Programmatically

I have a requirement to display a theme that comes from a webservice (return the color etc). What I want to know is can I create a style programmatically? I have my code setup to incorporate theme and I have 2 themes defined in xml which work perfectly fine. And now I want to add a third theme which is not locally in an xml. Is this even possible?
The other approach to achieve this is to create custom classes for views and apply the style according to the theme. Which is I don't want to do, as it is very tedious and error prone.
Any other suggestions are welcomed as well.

Android easy switch color-scheme of application

How would you implement different color themes in your app?
All I can see now is plain set color onCreate every activity and control...
Also, how would you store different color schemes in xml?
Just an entries of with different names?
Thank you!
Use custom Themes, which are declared in XML. They are very similar to CSS, if you've used them before.
EDIT:
Here's a better example of changing the theme at run time

Categories

Resources