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.
Related
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
A similar question to this has been asked many times; however, an answer has not been given that addresses my situation. I need to dynamically change an application's theme based on color values that are being returned from an API call. I then need to change the theme colors of the app based on the values returned. Therefore, I have no way of saving the colors in a style XML file. Can this be done?
I have a base activity, and my plan is to set the app theme from there for all the activities.
Unfortunately, I did not find an easy way to do this. I created a ThemeColor class which holds all the colors returned by the API. Then for each activity I have to go through every widget and style it.
Example:
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(themeColor.getActionBarColor)));
this.getWindow().getDecorView().setBackgroundColor(Color.parseColor(themeColor.getBackgroundColor()));
etc
I was not able to locate a simple way to resolve this issue either. By creating a ThemeColor class that holds all the colors returned from the API. Next for each activity I needed to address each widget separately as well as style it.
How can I change the theme of an Android app in an automated way?
To set the theme dynamically at runtime, call setTheme() in your activity's onCreate() method, before calling setContentView(). To change the theme, you simply need to restart your activity.
Here is a nice tutorial on how dynamically apply themes.
and this one too.
I want to have a "change theme" feature in my app. If I call setTheme() in onCreate(), there is one problem.
The moment after I launch my app, a plain white background appears for a second (because I have set light theme in the manifest). After that, the complete layout of my Activity is displayed - it is either with white or black background, according to user's theme preference.
Is there any way I can change whether white or black background appears after launch?
Make sure you call setTheme() in onCreate() BEFORE calling setContentView(). Then if you want to dynamically change the theme again later, you should simply restart your activity.
If you are adding a theme to the entire program than you could start by doing:
In your manifest you add to your application tag that you are using a theme.
<application android:theme="#style/mythemename">
Then look at Theme XML to make sure that you have what you need declared in the appropriate places.
If it is just for a particular action you could add the activity tag
<activity android:theme="#android:style/Theme.propertyname">
You can also, if you want your theme to just change the background color, follow the same pattern with either the activity or application tag (what ever one you are using) and set the item name "colorbackground" to what you want.
You can also use Theme XML and remake what you want in your current theme and call that your custom theme using the method above.
I hope this helps and if not please let me know so I might be able to help better in the future.
Another way would be to have a kind of splash screen which will check for a preference variable for example and then decide whether to use the light or the dark theme. This way you could also use XML layouts.
EDIT: Yet another way is to have the all the layout defining stuff in the onCreate() method and then just trigger the onStart() method when ready.
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