The resources as R.colors
are generated at compile time as constants so you can not change them at run time. What you can do is create a function that returns the color you want and that all the views that require changing the color dynamically obtain it through the method.
For example.
Create a class Activity
base% for your entire application that contains the following method:
public ActivityBase extends AppCompactActivity
{
private boolean colorClaro = false;
public int obtenerColorDinamico()
{
if(colorClaro)
{
return super.getColor(R.colors.color_claro);
}
else{
return super.getColor(R.colors.color_oscuro);
}
}
}
So whenever a view requires the color to change, then you only assign the result of the function, for example, even TextView
:
public class HomeActivity extends ActivityBase
{
public void onCreate(Bundle instance)
{
setContentView(R.layout.layout_activity);
myTextView.setTextColor(obtenerColorDinamico());
//...
}
}
In the example, the color of the TextView
is based on the state of the variable colorClaro
, if it changes, when you load the view again all the elements that get the color of the function will change.
The state that will decide whether to take one color or the other you can save it in a SharePreferences
or database or as you choose.