I have a ListPreference with two possible string values ​​in PrefActivity . There is also a colors.xml file, where some colors are described, which are later used in other style files.

Question: how to change colors depending on the value from Preference in colors.xml ? There is a suspicion that you need to use selector , but I do not exactly know how to do it.

    1 answer 1

    The task is not solved in one move: selector knows nothing about preferences. You will have to code: get the selected value from the preferences, select the corresponding color value or its id in R.color, set this color to the corresponding element.

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

    If there are a lot of places like this, you will need to make a helper class to work with color. Or make a custom view in which to hide the work with color.

    • Can I have more detail on the item "If there are many such places"? - Evgeniy
    • Well, a lot, in the simple everyday sense. You notice that this setBackgoundColor is copy-paste in code for the thirtieth time. It has long been a lot. :) As for the question, it is not clear how your interface works and how many colors you need to change. - tse
    • Apparently, you misunderstood my comment) I meant to tell in more detail about the implementation of the helper class or what you mean by the "custom view", since for each element of each activation, separately register the background color is too hemorrhoid. - Evgeniy