Android Preference Settings Tutorial

Mostly all mobile app have their own settings to let user control their application so it can provide more user experience to them. In android development, the preference settings is different with android activity which using approach to create them. Preference settings layout is create in the xml folder while android activity is create in the layout folder. They both using different xml code to create their own layout while both allow to customize what design they want to. In this tutorial, I will teach you how to create android preference settings and do some simple settings in the android application.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “SettingsExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Create a XML folder

Go to your res folder > right click > new > new resource directory > resource type : xml and click ok.

settings1-a

Add a new xml file into xml folder

Right click xml folder and create one xml file “app_preferences” for the file name. After that edit it as the source code below. PreferenceCategory in the short term is section of preference.

Create a new empty activity

Right click your package name > new > activity > empty activity. Change the activity to “SettingsActivity” then click ok, it will auto create a activity and layout.

Edit activity_settings.xml layout

Go to the activity_settings.xml and edit to the source code below. I add the toolbar and framelayout for preferencefragment.

Edit SettingsActivity.java class

Go to this file and copy paste the source code that i provided in the below section. It will add settings fragment to the settings activity.

Edit activity_main.xml layout

In the activity_main.xml layout add the button to navigate to the settings activity so u can perform settings behaviour later.

Edit MainActivity.java class

Change the mainactivity.java file and create a new method call loadpreference to retrieve the changes made in the settings activity. You can get saved preference from getDefaultSharedPreferences().

Change styles.xml

Dont forget to change your styles to noactionbar, so it will no display 2 toolbar later.

Run your project

Now, you can run your project and test the settings now.

(Android Preference Settings)

Source Code

(Visited 612 times, 1 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *