Changing app theme at runtime using using external theme file - android

I want to produce a single app that would let the user select a theme and have this applied over the entire app.
I understand that this can be achieved by using setTheme in onCreate of each activity.
But I need this to work in a different way. I don't want to have the themes stored in theme.xml or styles.xml.
I want to have a list of themes stored on the web and be able to dynamically download a new theme and have it applied in the app. I want to be able to create new themes without having to build a new version or an updated version of the app.
Images would be easy to replace. Just download from a url and store locally to be re-used. But the actual theme of the app, the colours of buttons etc should be changed at run time from a theme.xml file which isn't part of the apk but is fetched online.
Is this possible?

It depends how much styling you want to be able to do. You currently can't set view items styles grammatically outside of using a resource. But you can control things like text color and background color. If that's all you need to change, I would recommend writing a Theme factory class for you app that you use to get each view element you need. For example a getButton() function that will return you a button with the background color and text color you need.

Related

Android material-date-picker, change colors without using theme

I'm using material date picker, and would like to change the accentcolor (for header background, selected date and buttons), but I don't know the color until runtime (as it's user defined and coming from an external source), so can't define a theme in advance.
(I'd rather not use an external library, as I'm using this in multiple places already and I want to keep things consistent.)
Any ideas?

Change Theme Dynamically from User Input in Android

I'm building a material design playground that can switch between the available pre-defined themes that can apply the color, typography and shapes to all the material design components throughout the app.
Right now, the user can choose available themes from the PreferenceScreen:
I got a NEW requirement that should allow the user to enter a value (eg. set color of colorPrimary). From the input, I would like to modify the Theme directly so that it could apply the change in the app.
How can I do this? I'm thinking of giving an option to the user something like below:
Here's a link to my sample material design playground app:
https://github.com/ciscosoriano/material-design-dynamic-theming
Seems like you need something like this android-how-to-change-application-theme-programmatically
link: android-how-to-change-application-theme-programmatically
This should be easy and there are lot of tutorials out there, don't forget to checkout
Basically what you need to do is:
apply statically defined theme(in res/values) as usual before activity is launched.
Now get the user input when activity is launched
Then get the current theme attributes using appropriate api and recolourize the ui controls dynamically in the program.
restart activity with the updated theme.
Some more link: Set theme color dynamically
You should be comfortable handling the activity lifecycle to view the updated theme when set.

Create a theme style dynamically in an android application

I'm looking for a solution to create a theme style dynamically in an android app. Our customer make there styles in an back office and from the application we retrieve this information. With this info's we wan't to update the current theme or create a new one. But we didn't found a way to do this because we can't add or change a data in sytle.xml or R class during running app.
I don't want to modify element by element on loading page in code like this textViewStyleDynamic.setTextColor(Color.BLUE).
It would be perfect to edit or create a theme dynamically and set it on the app.

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.

Edit AppTheme in Run Time, How can i do it programmatically without XML-code?

I have the following problem:
There is response with json-array, which contain colors settings from the server. And from difference companies difference settings. There are next colors:
1) A main color, it serves to fill background;
2) A second color, which highlights a text, icons and buttons on the background;
3) The color, the darker the first - used in the allocation;
4) The color that is used for coloring the boundaries of the text and icons, if 1st color matches with the 2nd;
(It is the analogy with colors on iOS, because we write native applications and have on server)
And I do not know whether you can manually set these colors in Theme for the entire application , or this configuration must be stored in the form of a XML-code, i.e. we need to write a parser that generated an xml-string (it is a difficult way for me =))?
In addition - if I want to change the Theme of entire application, how can I do this better programmatically? One way I know - all my activities inherited from class, which extends a class Activity, and in this class prescribe and change the Theme. Maybe there are other ways? But the main question about the generation of style.
Sadly, there is no clean and direct way to do this. The theme is part of the resources which are generated at compile time. And Resources.Theme is a final class, so it can't be overridden to do custom behaviors.
So the options you are left with are all bad:
Brute-force change each and every visual property of every widget affected by the customer theme. Yuck.
Dynamically create HTML pages that can be read into a WebView and used as the UI instead of Android widgets. You will probably have to write a lot of canned JavaScript code that will be part of this web UI.
The cleanest, but nearly impossible way, would go like this: As each customer gets registered in your corporate database (along with their color branding scheme), your build process generates a new expansion APK with a theme for that customer, then uploads it to Google Play. Your app would need to know to look for an updated expansion APK and get the user to download it (which may not always happen), then your JSON response would give you the customer name or whatever you need to determine which theme to pull from the expansion APK. Finally, your app would set the theme on the Activity for your white-label UI. So many things would have to happen correctly that it would be a minor miracle if your user saw your customers' corporate branding colors. But just thought I'd mention the idea for inspiration.

Categories

Resources