I've been struggling to implement PreferenceFragment in my app. My goal is to have the preferences view replace my main_activity fragment container so I can keep the same nav drawer, action bar, etc.
I have created a Preference Fragment class like so:
public class MadlibsSettings extends PreferenceFragment {
android.support.v7.app.ActionBar actionBar;
CheckBoxPreference themeSwitch;
ListPreference fontSize;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(android.R.color.white));
actionBar = (android.support.v7.app.ActionBar) ((MainActivity)getActivity()).getSupportActionBar();
actionBar.setTitle("Settings");
addPreferencesFromResource(R.layout.madlibs_settings);
//fontSize = (ListPreference) findPreference("list");
return view;
}
}
And my prefs in R.layout.madlibs_settings are:
<PreferenceCategory android:title="PreferenceCategory A" >
<CheckBoxPreference
android:id="@+id/checkbox1"
android:defaultValue="false"
android:key="checkbox1"
android:summary="Switches App Layout to Dark Theme"
android:title="Darker Theme" />
</PreferenceCategory>
<PreferenceCategory android:title="PreferenceCategory B" >
<ListPreference
android:id="@+id/ListPreference"
android:defaultValue="8"
android:entries="@array/list"
android:entryValues="@array/LValues"
android:key="list"
android:summary="Choose Font Size"
android:title="Font Size" />
</PreferenceCategory>
</PreferenceScreen>
I'm not really sure what to do in my main activity in order to inflate preferences and then access the data from my prefs usings haredpreferences. Any help would be great, I'm definitely a rookie with fragments.