Step by step guide to create App Settings using Preferences in Android (Part -1)

Bhavya Karia
3 min readMar 19, 2018

--

Many a times we need certain settings in our app, like setting default language, theme(light or dark), customizing notifications, etc. In this tutorial we will be creating a simple app with settings to change text visibility, text size and color. For this we will be using Shared Preferences.

Final Output:

Final App

In this tutorial we will be creating the first setting that is making the text visible or invisible by CheckBoxPreference and in next article we will dive into ListPreference and the EditTextPreference.

Step 1:

Layout Code for our Main Activity

Step 1:

Create a Settings Activity in which will add our settings menu. To create a menu and add items to it, first create a menu folder and add a menu resource file it.

android:orderInCategory: The order of "importance" of the item, within a group.

Step 2:

After creating the menu, we need to override two methods,

In onCreateOptionsMenu method we inflate our settings_menu file in our Main Activity and in the second method we setup an explicit intent to open up Settings Activity.

Step 3:

Now we will set the Settings Activity as the child activity of the Main Activity by making following changes in the Android Manifest file.

What is Parent Activity and Child Activity?

Up navigation

Up navigation, sometimes referred to as ancestral or logical navigation, is used to navigate within an app based on the explicit hierarchical relationships between screens. With Up navigation, your activities are arranged in a hierarchy, and “child” activities show a left-facing arrow in the action bar that returns the user to the “parent” activity.

Providing Up behavior for your app is optional, but a good design practice, to provide consistent navigation for the activities in your app.

android:launchMode=“singleTop”: When returning from Settings Activity, it does not recreates the instance of the activity instead calls the already existing.

Step 4:

For navigating back from the Settings Activity to Main Activity on pressing the back button on the Action bar, we need to add the following code to the SettingsActivity.java

Step 4:

Add following dependencies to your gradle file and sync it.

implementation 'com.android.support:preference-v7:26.1.0'

Create a Settings Fragment and extend it with PreferenceFragmentCompat and override the onCreatePreferences method.

Step 5:

Next we will create a xml folder in res and add a settings_pref.xml to it. Here we will create our settings menu.

For this tutorial we will be adding just a CheckBoxPreference to the settings menu.

Here,

In Shared Preferences we store data in key: value pair and while retrieving the data we will be needing the key.

All the Preferences will be having a key, title and a default value.

Now attach settings_pref.xml to the SettingsFragment.java and add this fragment to the SettingsActivity.java

Along with this we will need to add the following theme into styles.xml or else the app will crash.

<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

Step 6:

Now to read from the Shared Preferences and making changes to our text, we will make following changes to the MainActivity.java file.

First we got the instance of the Shared Preferences in the method setupSharedPreferences and registered it. Then we override the onSharedPreferenceChanged method and call the method responsible to change the visibility of the text.

Registering and unregistering the shared preferences is necessary for making dynamic changes to the text or else the changes will take place only on recreating the activity.

By now, we have implemented the first settings of making the text visible or invisible.

In future tutorial we will be adding the other two settings.

The code for this app can be found in my GitHub account,

--

--