title: How to change text color and background color of PreferenceScreen in Android
body: <p>How to change text and background color in a PreferenceScreen inflated by <strong>PreferenceFragmentCompat</strong>?</p>
  <p>Tried <a href="https://stackoverflow.com/questions/37935057/how-to-change-the-text-color-of-preferencecategory-preferencescreen/39920363#39920363">How to change the text color of PreferenceCategory/PreferenceScreen</a> but the solution works in a Preference Activity and not in PreferenceFragmentCompat.</p>
  <p>Also tried using the layout tag in the preference screen as per <a href="https://stackoverflow.com/questions/11607302/how-to-change-text-color-of-preference-category-in-android/34697171#34697171">How to change text color of preference category in Android?</a> but doesn't save preference.</p>
  <pre class="lang-kotlin prettyprint-override"><code>class FragmentSettings: PreferenceFragmentCompat() {
  
      private lateinit var viewModel: SharedViewModel
  
      override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
          addPreferencesFromResource(R.xml.root_preferences)
  
          val application = requireNotNull(this.activity).application
          val dataBase = DataBase.getInstance(application)
          val repo = Repo(dataBase!!)
  
          viewModel = ViewModelProvider(this,
              SharedViewModelFactory(
                  dataBase
              )
          ).get(SharedViewModel::class.java)
  
          (activity as MainActivity).supportActionBar?.title = &quot;Settings&quot;
  }
              
  </code></pre>
  <p>This is the code in my settingsPreference.xml.</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;PreferenceScreen xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:background=&quot;@color/colorExtra&quot;
      android:backgroundTint=&quot;@color/colorExtra&quot;&gt;
  
      &lt;PreferenceCategory android:layout=&quot;@layout/pref_title&quot; app:title=&quot;@string/location_header&quot;&gt;
  
          &lt;SwitchPreferenceCompat
              android:background=&quot;@color/colorExtra&quot;
              android:backgroundTint=&quot;@color/colorExtra&quot;
              app:defaultValue=&quot;true&quot;
              android:layout= &quot;@layout/switch_pref_item&quot;
              app:disableDependentsState=&quot;true&quot;
              app:key=&quot;USE_DEVICE_LOCATION&quot;
              app:summary=&quot;Allow the app to get your location&quot;
              app:title=&quot;@string/your_location_title&quot; /&gt;
  
          &lt;EditTextPreference
              android:background=&quot;@color/colorExtra&quot;
              android:backgroundTint=&quot;@color/colorExtra&quot;
              app:dependency=&quot;USE_DEVICE_LOCATION&quot;
              app:key=&quot;setLocation&quot;
              android:layout=&quot;@layout/set_location_pref&quot;
              app:title=&quot;@string/set_location_title&quot;
              app:useSimpleSummaryProvider=&quot;true&quot; /&gt;
  
  
      &lt;/PreferenceCategory&gt;
  
  &lt;/PreferenceScreen&gt;
  </code></pre>