ListPreference Example

This example shows how you can use list preference in android. ListPreference A Preference that displays a list of entries as a dialog. This preference will store a string into the SharedPreferences.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it ListPreferenceExample.

2.) Write following into main.xml:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<Button
android:id="@+id/setpreference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Set Preference" />
<TextView
android:id="@+id/setting_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/setting_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

 

3.) Create and write following into res/xml/preference.xml:

 

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

<PreferenceCategory
android:title="PreferenceCategory CheckBoxPreference">
<CheckBoxPreference
android:key="PREF_CHECKBOX"
android:title="Title"
android:summary="summary" />
</PreferenceCategory>

<PreferenceCategory
android:title="PreferenceCategory ListPreference">
<ListPreference
android:key="PREF_LIST"
android:title="ListPreference title"
android:summary="ListPreference summary"
android:entries="@array/listentries"
android:entryValues="@array/listvalues" />
</PreferenceCategory>

</PreferenceScreen>

 

 

4.) Create and write following into res/values/arrays.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listentries">
<item>List item 1</item>
<item>List item 2</item>
<item>List item 3</item>
<item>List item 4</item>
</string-array>
<string-array name="listvalues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
</resources>

 

 

5.) Add following activity to your manifest.

<activity android:name="PrefActivity"></activity>

 

6.) Create and write following into PrefActivity.java:

 

package com.example.ListPreferenceExample;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class PrefActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

PrefFragment prefFragment = new PrefFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, prefFragment);
fragmentTransaction.commit();
}
}

 

7.) Create and write following into PrefFragment.java:

 

package com.example.ListPreferenceExample;

import android.os.Bundle;
import android.preference.PreferenceFragment;

public class PrefFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
}
}

 

8.) Run for output.

Steps:

1.) Create a project named ListPreferenceExample and set the information as stated in the image.

Build Target: Android 4.3
Application Name: ListPreferenceExample
Package Name: com.example. ListPreferenceExample
Activity Name: ListPreferenceExampleActivity

2.) Open ListPreferenceExampleActivity.java file and write following code there:

 

package com.example.listpreferenceexample;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ListPreferenceExampleActivity extends Activity {

Button buttonSetPreference;
TextView settingCheckBox, settingList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSetPreference = (Button)findViewById(R.id.setpreference);
settingCheckBox = (TextView)findViewById(R.id.setting_checkbox);
settingList = (TextView)findViewById(R.id.setting_list);

buttonSetPreference.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
Intent intentSetPref = new Intent(getApplicationContext(), PrefActivity.class);
startActivityForResult(intentSetPref, 0);
}});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Boolean prefCheckBox = sharedPreferences.getBoolean("PREF_CHECKBOX", false);
settingCheckBox.setText("CHECKBOX preference = " + prefCheckBox.toString());
String prefList = sharedPreferences.getString("PREF_LIST", "no selection");
settingList.setText("LIST preference = " + prefList);
}
}

 

3.) Compile and build the project.

Output