Search code examples
androidandroid-fragmentssharedpreferencespreferencespreferencefragment

Implementing PreferenceFragment?


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.


Solution

  • Because the preferenceFragment is an actual Fragment, you can simply exchange it using a FragmentTransation as you would with your other fragments in your Nav drawer. In whatever onClick event or otherwise, use something like the following to go to your PreferenceFragment:

    getFragmentManager().beginTransaction()
                .replace(R.id.fragmentContainer, new MadlibSettings())
                .commit();
    

    Source: http://developer.android.com/guide/topics/ui/settings.html#Fragment

    And then for accessing your preferences from anywhere, the following code should be able to get you started.

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getContext());
    String val1 = settings.getString("KEY", "default_value");
    settings.putString("key", "new_value");
    

    If you decide to make your own preferences file in addition to the settings one, then you will use:

    SharedPreferences settings = getContext().getSharedPreferences("pref_file_name", 0);
    String val1 = settings.getString("KEY", "default_value");
    settings.putString("KEY", "new_value");