I've made a function to change the colors of drawables in android. The problem is that it works only 60-70% of the times. I call it on the onCreate method of each activity of my app. (The color is saved in the shared preferences) As you can see in this image the color of the background drawable did not change It works mostly fine. But sometimes in one odd activity only the colors of some drawables change, and the others retain their default color. This is the code of the function:
public static void changeTheme(Context context) {
int color = Color.parseColor(AppUtils.getColorSharedPreference(context, AppConstants.customCOLOR));
if (color != -1) {
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.ic_untitled_4);
Drawable wrappedDrawable = null;
if (unwrappedDrawable != null) {
wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
}
Drawable unwrappedDrawableExit = AppCompatResources.getDrawable(context, R.drawable.logout_img);
Drawable wrappedDrawableExit = null;
if (unwrappedDrawableExit != null) {
wrappedDrawableExit = DrawableCompat.wrap(unwrappedDrawableExit);
}
Drawable unwrappedDrawableFocus = AppCompatResources.getDrawable(context, R.drawable.focus_img);
Drawable wrappedDrawableFocus = null;
if (unwrappedDrawableFocus != null) {
wrappedDrawableFocus = DrawableCompat.wrap(unwrappedDrawableFocus);
}
Drawable unwrappedDrawableSettings = AppCompatResources.getDrawable(context, R.drawable.settings_img);
Drawable wrappedDrawableSettings = null;
if (unwrappedDrawableSettings != null) {
wrappedDrawableSettings = DrawableCompat.wrap(unwrappedDrawableSettings);
}
Drawable unwrappedDrawableHome = AppCompatResources.getDrawable(context, R.drawable.home_img);
Drawable wrappedDrawableHome = null;
if (unwrappedDrawableHome != null) {
wrappedDrawableHome = DrawableCompat.wrap(unwrappedDrawableHome);
}
Drawable unwrappedDrawableBackArrow = AppCompatResources.getDrawable(context, R.drawable.arrow);
Drawable wrappedDrawableBackArrow = null;
if (unwrappedDrawableBackArrow != null) {
wrappedDrawableBackArrow = DrawableCompat.wrap(unwrappedDrawableBackArrow);
}
Drawable unwrappedDrawableUnknownUser = AppCompatResources.getDrawable(context, R.drawable.unknown_user);
Drawable wrappedDrawableUnknownUser = null;
if (unwrappedDrawableUnknownUser != null) {
wrappedDrawableUnknownUser = DrawableCompat.wrap(unwrappedDrawableUnknownUser);
}
Drawable unwrappedDrawableVerified = AppCompatResources.getDrawable(context, R.drawable.verified_user);
Drawable wrappedDrawableVerified = null;
if (unwrappedDrawableVerified != null) {
wrappedDrawableVerified = DrawableCompat.wrap(unwrappedDrawableVerified);
}
Drawable unwrappedDrawableNoFace = AppCompatResources.getDrawable(context, R.drawable.no_face_detected);
Drawable wrappedDrawableNoFace = null;
if (unwrappedDrawableNoFace != null) {
wrappedDrawableNoFace = DrawableCompat.wrap(unwrappedDrawableNoFace);
}
Drawable unwrappedDrawableWarning = AppCompatResources.getDrawable(context, R.drawable.warning);
Drawable wrappedDrawableWarning = null;
if (unwrappedDrawableWarning != null) {
wrappedDrawableWarning = DrawableCompat.wrap(unwrappedDrawableWarning);
}
Drawable unwrappedDrawableMessage = AppCompatResources.getDrawable(context, R.drawable.message_new);
Drawable wrappedDrawableMessage = null;
if (unwrappedDrawableMessage != null) {
wrappedDrawableMessage = DrawableCompat.wrap(unwrappedDrawableMessage);
}
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(color));
DrawableCompat.setTintList(wrappedDrawableBackArrow, ColorStateList.valueOf(color));
DrawableCompat.setTintList(wrappedDrawableExit, ColorStateList.valueOf(color));
DrawableCompat.setTintList(wrappedDrawableHome, ColorStateList.valueOf(color));
DrawableCompat.setTintList(wrappedDrawableSettings, ColorStateList.valueOf(color));
DrawableCompat.setTintList(wrappedDrawableFocus, ColorStateList.valueOf(color));
DrawableCompat.setTintList(wrappedDrawableUnknownUser, ColorStateList.valueOf(color));
DrawableCompat.setTintList(wrappedDrawableVerified, ColorStateList.valueOf(color));
DrawableCompat.setTintList(wrappedDrawableNoFace, ColorStateList.valueOf(color));
if (wrappedDrawableWarning != null) {
DrawableCompat.setTintList(wrappedDrawableWarning, ColorStateList.valueOf(color));
}
if (wrappedDrawableMessage != null) {
DrawableCompat.setTintList(wrappedDrawableMessage, ColorStateList.valueOf(color));
}
}
}
And this is a sample call:
if(Color.parseColor(AppUtils.getColorSharedPreference(LoginActivity.this,AppConstants.customCOLOR))!=-1)
{
AppUtils.changeTheme(LoginActivity.this);
}
Okay so I found out the real problem. this method needs to be called before super.onCreate();