1

i'm trying to build the settings for an app and i want to make it customizable, letting the user decide between light and dark theme. I already did this https://www.hidroh.com/2015/02/16/support-multiple-themes-android-app/ following this tutorial.

The problem is, i want another option to select the accent color, like many apps do. How can i achieve this independently from dark/light? Is there a way to avoid the restart of the activity?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
m.i.n.a.r.
  • 922
  • 2
  • 12
  • 28

1 Answers1

1

The approach I have used is that, keep your colors in an array and let users select the color and store the index of selected color in preferences. So once when the activity is loading read the stored color and set the color accordingly

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this,colors.getResourceId(chapter_key-1, 0)));
    }

main screen where they select the book

package com.**********.app.customViews;

/**
 * Created by pakistantechhouse on 18/02/2017.
 */

import android.content.Context;
import android.graphics.Canvas;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.**********.app.R;


public class CRelativeLayout extends RelativeLayout {


    public CRelativeLayout(Context context) {
        super(context);
        if (!isInEditMode()) {
            //TODO get your color here from the preferences and apply to the view
            this.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
        }
    }

    public CRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode()) {
            //TODO get your color here from the preferences and apply to the view 
            this.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));

        }
    }

    public CRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (!isInEditMode()) {
            //TODO get your color here from the preferences and apply to the view 

            this.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
        }
    }

    protected void onDraw (Canvas canvas) {
        super.onDraw(canvas);

    }

}
Ussaid Iqbal
  • 786
  • 1
  • 7
  • 16
  • Thanks but i'm not sure i understood. Do i need to restart the activity? And once the theme is loaded, i set the colors via Java code? – m.i.n.a.r. May 27 '18 at 15:22
  • Yes exactly, you set the theme first for that the app needs to restart. But for other colors you can just set it through java and you don't need to restart the activity. – Ussaid Iqbal May 27 '18 at 15:28
  • Just tried, but i want to change the accent color, not the statusbar color. Seems like i can't change this color via code ( https://stackoverflow.com/questions/27595625/how-to-set-coloraccent-in-code ), so the only way is work with styles. Some apps can change the color without an activity restart though (for example, phonograph). I'm a bit confused. – m.i.n.a.r. May 28 '18 at 17:14
  • https://stackoverflow.com/a/26511725/6386738 kindly refer to this, the approach I use is that I create custom views like textviews and layouts and I change the color as per user selection. – Ussaid Iqbal May 28 '18 at 17:18