Preference Fragments

This example shows how you can use preference fragment in android.

Algorithm:

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

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" />
</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 A">
<CheckBoxPreference
android:key="PREF_CHECKBOX"
android:title="Title"
android:summary="summary" />
</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.PreferenceFragmentExample;

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.PreferenceFragmentExample;

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 PreferenceFragmentExample and set the information as stated in the image.

Build Target: Android 4.3
Application Name: PreferenceFragmentExample
Package Name: com.example. PreferenceFragmentExample
Activity Name: PreferenceFragmentExampleActivity

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

 

package com.example.preferencefragmentexample;

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 PreferenceFragmentExampleActivity extends Activity {
Button buttonSetPreference;
TextView settingCheckBox;

@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);
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());
}
}

 

3.) Compile and build the project.

Output