Make A PreferenceFragment

github:ย https://github.com/GinaHsu/ud851-GinaExercises/commit/7c7d40661445421ed3642eae266749c02db78585

  • Add the gradle dependency for the support preference fragment
  • Create the SettingsFragment class that extends PreferenceFragmentCompat
public class SettingsFragment extends PreferenceFragmentCompat {
}

 

  • Create the pref_visualizer in res->xml
  • Add a CheckBoxPreference to the preference
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:defaultValue="true"
        android:key= "show+bass"
        android:summaryOff="Hidden"
        android:summaryOn="Shown"
        android:title= "Show Bass"/>
</PreferenceScreen>
  • Call addPreferencesFromResource method to add the preference file
public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.pref_visualizer);

    }
}
  • Add a preference theme to styles.xml
<item name= "preferenceTheme">@style/PreferenceThemeOverlay </item>

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s