How to make a simple settings page in android?

11,704

Solution 1

Use PreferenceActivity

sample code from the developer site:

public class PreferenceWithHeaders extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Add a button to the header list.
        if (hasHeaders()) {
            Button button = new Button(this);
            button.setText("Some action");
            setListFooter(button);
        }
    }

    /**
     * Populate the activity with the top-level headers.
     */
    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }

    /**
     * This fragment shows the preferences for the first header.
     */
    public static class Prefs1Fragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Make sure default values are applied.  In a real app, you would
            // want this in a shared function that is used to retrieve the
            // SharedPreferences wherever they are needed.
            PreferenceManager.setDefaultValues(getActivity(),
                    R.xml.advanced_preferences, false);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.fragmented_preferences);
        }
    }

    /**
     * This fragment contains a second-level set of preference that you
     * can get to by tapping an item in the first preferences fragment.
     */
    public static class Prefs1FragmentInner extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Can retrieve arguments from preference XML.
            Log.i("args", "Arguments: " + getArguments());

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.fragmented_preferences_inner);
        }
    }

    /**
     * This fragment shows the preferences for the second header.
     */
    public static class Prefs2Fragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Can retrieve arguments from headers XML.
            Log.i("args", "Arguments: " + getArguments());

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preference_dependencies);
        }
    }
}

The preference_headers resource describes the headers to be displayed and the fragments associated with them. It is:

<header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1Fragment"
        android:icon="@drawable/ic_settings_applications"
        android:title="Prefs 1"
        android:summary="An example of some preferences." />

<header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs2Fragment"
        android:icon="@drawable/ic_settings_display"
        android:title="Prefs 2"
        android:summary="Some other preferences you can see.">
    <!-- Arbitrary key/value pairs can be included with a header as
         arguments to its fragment. -->
    <extra android:name="someKey" android:value="someHeaderValue" />
</header>

<header android:icon="@drawable/ic_settings_display"
        android:title="Intent"
        android:summary="Launches an Intent.">
    <intent android:action="android.intent.action.VIEW"
            android:data="http://www.android.com" />
</header>

The first header is shown by Prefs1Fragment, which populates itself from the following XML resource:

<PreferenceCategory
        android:title="@string/inline_preferences">

    <CheckBoxPreference
            android:key="checkbox_preference"
            android:title="@string/title_checkbox_preference"
            android:summary="@string/summary_checkbox_preference" />

</PreferenceCategory>

<PreferenceCategory
        android:title="@string/dialog_based_preferences">

    <EditTextPreference
            android:key="edittext_preference"
            android:title="@string/title_edittext_preference"
            android:summary="@string/summary_edittext_preference"
            android:dialogTitle="@string/dialog_title_edittext_preference" />

    <ListPreference
            android:key="list_preference"
            android:title="@string/title_list_preference"
            android:summary="@string/summary_list_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="@string/dialog_title_list_preference" />

</PreferenceCategory>

<PreferenceCategory
        android:title="@string/launch_preferences">

    <!-- This PreferenceScreen tag sends the user to a new fragment of
         preferences.  If running in a large screen, they can be embedded
         inside of the overall preferences UI. -->
    <PreferenceScreen
            android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"
            android:title="@string/title_fragment_preference"
            android:summary="@string/summary_fragment_preference">
        <!-- Arbitrary key/value pairs can be included for fragment arguments -->
        <extra android:name="someKey" android:value="somePrefValue" />
    </PreferenceScreen>

    <!-- This PreferenceScreen tag sends the user to a completely different
         activity, switching out of the current preferences UI. -->
    <PreferenceScreen
            android:title="@string/title_intent_preference"
            android:summary="@string/summary_intent_preference">

        <intent android:action="android.intent.action.VIEW"
                android:data="http://www.android.com" />

    </PreferenceScreen>

</PreferenceCategory>

<PreferenceCategory
        android:title="@string/preference_attributes">

    <CheckBoxPreference
            android:key="parent_checkbox_preference"
            android:title="@string/title_parent_preference"
            android:summary="@string/summary_parent_preference" />

    <!-- The visual style of a child is defined by this styled theme attribute. -->
    <CheckBoxPreference
            android:key="child_checkbox_preference"
            android:dependency="parent_checkbox_preference"
            android:layout="?android:attr/preferenceLayoutChild"
            android:title="@string/title_child_preference"
            android:summary="@string/summary_child_preference" />

</PreferenceCategory>

Note that this XML resource contains a preference screen holding another fragment, the Prefs1FragmentInner implemented here. This allows the user to traverse down a hierarchy of preferences; pressing back will pop each fragment off the stack to return to the previous preferences.

See PreferenceFragment for information on implementing the fragments themselves.

Solution 2

As of 2019 the recommended way to do this is to use the AndroidX Preference Library.

PreferenceActivity is actually deprecated in API Level 29 (source):

This class was deprecated in API level 29. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings.

For a working minimal example please refer to this example in the official docs.

Share:
11,704
DominikTV
Author by

DominikTV

From Germany 23 years old Android User Android "Hobby programmer", I code just for fun :) I &lt;3 Google's Material Design Media student Languages Java (Android) PHP (and HTML) SQL C++ (Basics)

Updated on June 28, 2022

Comments

  • DominikTV
    DominikTV almost 2 years

    I'm new in the "Android-App-Dev"-Scene and got one question:

    How do I easily make a good clean looking settings page for my app?

    There are some kind of headlines and some kind of big buttons on you can tab to go to a new page.

    I'm using Android Studio and know how to create a new page, class etc..

  • T.Todua
    T.Todua over 7 years
    plz, do me a favour (as i am newbie) and write where those codes should be written (in which files).
  • T.Todua
    T.Todua over 7 years
    also, how a button should call / open that page.
  • T.Todua
    T.Todua over 7 years
  • Josip Domazet
    Josip Domazet over 4 years
    I love how I get downvoted even tho I am literally quoting the official documentation.
  • thatGuy
    thatGuy over 3 years
    That's true. I still see people referring the deprecated api.
  • Arlen Beiler
    Arlen Beiler almost 2 years
    9 to 1 isn't bad, but here's another +1 :)