-2

I am working on a project which is having customization functionality. Where client can choose there Application's color and that I need to reflect in my App. I am done with API:

{
"code": 200,
"msg": "Ok",
"status": "success",
"color": {
  "toolbar": "#FFFFFF",
  "button_color": "#FFFFFF",
  "button_text": "#FFFFFF",
  "status_bar": "#FFFFFF",
  "section_title_text": "#FFFFFF",
  "banner_divider": "#FFFFFF",
  "nav_divider": "#FFFFFF",
  "app_background": "#FFFFFF"
 }
}

I did JSON parsing too. But the thing is how to set these color? Like how to set "toolbar" color to ColorPrimary? how to set "status_bar" color to "colorPrimaryDark" and so on?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

what you want to do is to change colors values in color resource file according to your JSON response, actually that is NOT possible, as resource files can't be changed programmatically because it is already compiled before run time (refer here).

What you can do is to get the color value you want then save it to the app sharedPreferences, and then set the background color of all of the views that required to use that color from sharedPreferences saved color not from resources file (refer here).

To save the color to SharedPreferences:

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("primaryColor", primaryColorValue);
editor.apply();

To retrieve the color from sharedpreference:

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String colorValue = sharedPreferences.getString("primaryColor", "#FFFFFF");

to get the color from the String value:

int color = Color.parseColor(colorValue);

then use the color variable to set the color to the different views.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
0

you can set color as per your choice by this code; change '#FFFFFF' value to dynamic value from json data you have..

int color_default = Color.parseColor("#FFFFFF");
ActionBar.setBackgroundDrawable(new ColorDrawable(color_default));

Note : you can't change android color resources value dynamically through web services.

Milan Hirpara
  • 534
  • 4
  • 18