I am trying to add a settings activity to my app as described in https://developer.android.com/guide/topics/ui/settings but after adding initial implementation, when my settings activity is loaded I get a "You need to use a Theme.AppCompat theme (or descendant) with this activity." exception.
Fragment Must specify preferenceTheme in theme onCreate You need to use a Theme.AppCompat theme (or descendant) with this activity
According to https://developer.android.com/jetpack/androidx/releases/archive/androidx this issue should not occur as of 1.0.0-rc01 androidX release and yet I am experiencing it.
as workarounds i tried to hardcode the theme for the settings activity in the activity's xml layout file, and also tried to add a preferenceTheme item to my app's styles.xml. none worked.
/* settings activity + fragment */
public class SettingsPrefActivity extends AppCompatActivity {
private static final String TAG = SettingsPrefActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// load settings fragment
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, new MainPreferenceFragment())
.commit();
}
public static class MainPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.pref_main, rootKey);
}
}
My styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
to clarify I also tried specifically adding the following which didn't resolve the issue.
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>