Android Change Color Resource Value Programmatically - android

I am making an app and was wondering if there was a way to change a color resource value programmatically. For example, I am using the resource R.color.text_color_name to set the TextView text color. Is there a way to set that value to something different so that it would change the color of every TextView in the application?

The R file contains constants, you cant change them at runtime because you can't normally change constants at run time. If you want to update the color of all textviews why dont you look into themes. Create a custom theme and then change the theme when activity loads in onCreate. After you set the new theme i think your going to have to call setContentView again and then call all your findViewByIds again as they will be null. You could also try Calling recreate() after setTheme(). This sounds messy.
Maybe this can help you change the theme.

Related

How can I change the predefined color in Android with xml

Its not so long since I started to program in Android Studio. I was wondering why everything that I'm adding (like button, the actionBar...) has already a Lilac predefined color and how I can change this color. When I tried to change the color of the button in xml of my activity nothing happened.
android:background="#color/teal_200"
Can you help me please to know where I can change this default color and why the
android:background="#color/teal_200"
didn't worked?
Thank you!
These elements will likely be in an activity and that activity will have a theme, which you can check by looking at the android:theme of the activity (or the application, if the activity doesn't have one) in AndroidManifest.xml.
If you've started a new project from a template, this theme will likely be in res/values/themes.xml and it will define colours that will be used wherever that theme is applied.
Different elements take different colours from the theme and you need to check the documentation for each one to understand where their colours come from and how to change them.
For example, if you're using a contained MaterialButton, its documentation is here. There you can see that it takes its background colour from the colorPrimary of the theme by default and this can be overridden by setting the app:backgroundTint of the button. Therefore, if you wanted to change the colour of all the contained buttons in activities that use that theme, you'd need to change the colorPrimary of the theme. You could also change the colour of individual buttons by setting the app:backgroundTint of each button.
Note that several UI elements also use the colours set in the theme (like colorPrimary) and they'll change if you change those values. There's more info about what the colours in the theme are used for here, here and here and more general info about themes and styles here.
please change in colors.xml in your resource file
Open values -> colors.xml and add the color you want and set it in the layout XML file

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.

Modify Android Theme Programmatically

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.

Implement change black/light theme feature in Android app

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.

Categories

Resources