androidpreferenceactivitypreferencefragment

How to create settings screen in android app?


How do I create a settings screen? Should I use PreferenceActivity or PreferenceFragment?

Note- Documentation wasn't helpful

What I've already tried:

  1. Using PreferenceActivity but couldn't implement a simple toolbar in it.
  2. Using PreferenceFragment but was too complex to understand.

What I want is a simple implementation using PreferenceFragment


Solution

  • On Android 3.0 and later, you should use a PreferenceFragment that displays your app settings.

    Create a SettingsFragment:

    public static class SettingsFragment extends PreferenceFragment {
       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
    
          // Load the preferences from an XML resource
          addPreferencesFromResource(R.xml.preferences);
       }
       ...
    }
    

    And show it in your activity, like any other fragment:

    public class SettingsActivity extends Activity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_settings);
          // Display the fragment as the main content.
          getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, new SettingsFragment())
                    .commit();
       }
    }
    

    You can include a toolbar in your activity xml, and add a container for SettingsFragment under it, this way the toolBar and your SettingsFragment will both be shown)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_catalog_root_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="-20dp">
    
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/fragment_catalog_first_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#00000000"
            app:elevation="0dp"
            >
    
            <androidx.appcompat.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/fragment_catalog_first_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/transparent_background"
                android:elevation="4dp"
                >
    
            </androidx.appcompat.widget.Toolbar>
    
        </com.google.android.material.appbar.AppBarLayout>
    
        <FrameLayout
            android:id="@+id/activity_catalog_fragment_cont"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?android:attr/actionBarSize"
            android:animateLayoutChanges="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </RelativeLayout>
    

    If you don't use androidx (but you'd better use), you can use toolbar in your xml like:

    <android.support.v7.widget.Toolbar.....>
    

    Notice: you need to use an attribute android:layout_marginTop="?android:attr/actionBarSize" (or if you're using the AppCompat library: ?attr/actionBarSize) for your fragment container will appear under toolbar

    Within your activity don't forget to override onClick() method for your toolbar:

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           getActivity().onBackPressed();
        }
    });