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.
Related
Problem :
I am trying to change the look and feel of my Android app, on the fly. Something like, the app starts up and gets a set of values from the server. These values are the elements that typically go inside colors.xml. What I am looking for, is a way to dynamically change the elements inside the colors.xml and update it with these new values received from the server. My understanding is that normally, this cannot be done directly. But has anyone found a workaround?
What I want to avoid if possible :
I would like to avoid setting color values inside each activity's onCreate() method for each element in that view. If at all possible, I would like to avoid this.
Any thoughts?
You can achive this change by newly introduce firebase remote config which provide remote config to change theme color or any other values necessary for app like promotion,updates etc
You can refer this Example
Unfortunately all color values (and other resources) inside the resources directory are hardcoded as static final ints. This means there is no way to change the values at runtime. You can however use one of the previously suggested solutions or have a look at this excellent explanation: https://stackoverflow.com/a/33992017/3662251
For a nice workaround that overrides the activity's getResources method and implements a custom Resources class which is in my opinion the most seamless solution: https://stackoverflow.com/a/34178187/3662251
I have did that in my app getting Hex color code like #06FF67 from my server and stored in sharedpreferences - https://stackoverflow.com/a/23024962/4741746
And when need to set new value that coming from server just override same shared preferences value with new data and set to app
Or you can use Random Color genrater also -https://stackoverflow.com/a/5280929/4741746
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.
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.
I am developing an application that will be themed with different colors and images for different clients. While I have the option to re-write the colors.xml file with the custom colors at build time, I am leaning towards setting up the colors at runtime. What I am wondering is if that is some way to programmatically change the value of a color defined in the colors.xml file and have that new value take effect in ALL places where it is used in the layout definition.
So in other words if I have:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color headerColor="white">#FFF</color>
<color backgroundColor="black">#000</color>
</resources>
And a layout file with something like:
<TextView
android:id="#+id/listItemHeaderActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/headerColor"
android:background="#color/backgroundColor"
android:text="#string/listTextHeaderActivity"/>
Can I change the value of headercolor and backgroundColor in Java and have it take place in all Views that use those values? Or will I have to change each relevant View color individually whenever I display those views?
Thanks in advance.
It would help if you described why and how you are using this dynamic color, and why you want to do this at runtime instead of build time. But assuming it has to be the way you asked...
I suggest writing a helper function that all places can call and set them.
public int getMyColor(...) {
// figure out which color to use, via a database call,
// an asset load, some algorithm, or whatever you need
...
// once color chosen, create an RGBA integer for it
final int myColor = ...
return myColor;
}
Now call this on every activity/fragment that needs it and set the color attribute(s) on the appropriate views as needed. (With View.setBackgroundColor(...), etc.)
However, to allow this to work in XML settings and/or development layout previews, you would have to write a custom view class to call that helper function too. Depending on where and how you will be using this color, it may not be worth it.
This solution isn't very elegant and requires a lot of calling this custom getMyColor helper function in every activity/fragment that needs it. If it is only set one or two places though, it's probably not a big deal. Again, knowing why you want to do this instead of asking us how to do it, may yield a better alternative for you.
For example—this isn't an answer to your question—but have you thought about themes? It will still have the problem of having to set them at build time unless you want all of the above, but depending on how you're using this color, it might be better than the mess I outlined above.
I ended up doing something somewhat similar to what I think Jon may be describing. I created a class extending Fragment (or SherlockFragment in my case). That class has a new method called "setCustomColors" that just takes the fragment view and searches all of its children for certain types of views that need to be customized. I then extended that class for all of my Fragments, and in the onCreateView method, I call the function, passing in the current fragment view. Let me know if that explanation is unclear (although maybe this isn't a problem very many people will have).
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