-1

I've a preferenceactivity that load settings to my app from a xml resource
addPreferencesFromResource(R.xml.secondary_settings); I would like to change the items background color individually and programatically, how can i do it?

enter image description here

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105

1 Answers1

0

I did solve my problem thanks to myself and not to google.

From all the questions you get about preference.getView here and everywhere you kind of realizes that it doens't work for nothing and indeed in google's standard implementation of Preference it is kind of useless, it will return you a NEW view everytime you call it, while the old view (used on PreferenceScreen) keep untouched...
so unless you are building the whole PreferenceScreen out of android framework, this method is pretty useless... But I found a workaround

First of all it only works if you are using the Compat libs, it took me 1 day of work until to make it work because of that...

so your gradle must: implementation "com.android.support:preference-v7:$support_library_version"
And your class must: public class SecondarySettingsFragment extends PreferenceFragmentCompat

After migrating it, from wherever in your activity you want to get the PreferenceView do ((RecyclerView)findViewById(android.support.v7.preference.R.id.recycler_view).getChildAt(index);

OBSERVATION, as it is a RecycleView it will have ZERO CHILDREN during onCreate() you must addOnGlobalLayoutListener or call your method from somewhere else

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105