Jetpack Compose theming at runtime from network api call - android

I have an API call for theming that returns all colors for my application. I want to apply these colors all of jetpack components. can anyone please suggest a proper way to do this.
I have two files theme. kt and color. kt .
I need to change values in these classes dynamically.

you should receive your colors from API then store that in dataStore then observe your data store in the main theme compose function whenever colors changed then create a new color pallet then set that in your MaterialTheme. automatically everywhere used MaterialTheme colors are updated.

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.

How to theme a Compose app with more colors than Material theme allows?

We have an app using Android View system that we want to start migrating to Jetpack Compose soon.
The app has some screens that look pretty different so that it doesn't seem to work to use a single Material theme for the whole app. We can't really figure out which color will be primary, secondary and variants. We have more colors than this.
So I see two alternatives: Either don't use Material theme but make own own custom theme for the whole app, or use Material theme but have two or three different themes so each screen can use the relevant one.
It seems pretty easy to for solution 2 (several Material themes in a single app) in Compose. But I'm wondering if it's a good practice, if there are cons that we haven't thought about?
Thanks for your advices!
Compose library has limited set of available colors in Colors class. If you want to have additional colors there are two solutions I can think of:
Create extensions for Colors class for each specific color. This is fast but downside of this method is that you will have to handle logic with theming inside of each color added. Sample
Create custom Colors class yourself and provide it using CompositionLocal . This way you won't need to include theme checks everywhere, you can do it only once and provide required Colors instance with CompositionLocalProvider

How can I change app colors programmatically?

I want change to my primary colors at runtime. I can change it with custom themes and colors. But I want to change my color from service result not static styles code. Is there any way to change colors.xml programmatically.
I find a solution but that's deprecated now: https://stackoverflow.com/a/34178187/6155031
Create one Singleton class that defines all the colors that you want
to change in your app.
Set its properties on app load from JSON file
obtained from the cloud.
In your app, wherever you are using color codes, don't set those values in xml. Set color values in your Java/Kotlin code.
I assume you are aware of findViewbyId. :-)

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.

Categories

Resources