Pr.Pg Next Pg

Preferences tutorials

 

What is a Preference

  • Shared preferences are simply sets of data values that are stored persistently.

  • By persistence, we are talking about data that persists across application lifecycle events.

  • In other words, the application (or device, for that matter) can be started and stopped without losing the data.

  • The next time the user launches the application, that data will still be available.

 

Setting up the Preferences in our Application

  • We will have to create xml folder in res folder.

  • We then create new XML file in our xml folder. Change the type of resource to Preferences.

  • We then enter the following code in that file as follows

 

 

prefs.xml

 

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

 

<EditTextPreference

android:title="Edit Text"

android:key="entered_text"

android:summary="Enter a Text"

 

/>

 

<ListPreference

android:title="List"

android:key="list"

android:entries="@array/list"

android:entryValues="@array/listValues"

android:summary="This is a list to choose from"

/>

 

Pr.Pg border                                              Next Pg