Lesson 111. Android 3. Fragments. PreferenceFragment - Settings. Headers

Lesson 111. Android 3. Fragments. PreferenceFragment – Settings. Headers

In this lesson:

– we work with PreferenceFragment
– we use Headers

IN Lessons 71-75 we were looking at a special kind of Activity – PreferenceActivity. It allows us to conveniently work with Preferences. For fragments there is an analogue – this is PreferenceFragment, which has the same functionality.

In Android 3, PreferenceActivity has a new feature – Headers. They allow large screens to easily display settings that are split into two panels vertically. If the screen is too small for this separation, then these panels will be on different screens.

Let’s create an application in which we use PreferenceFragment with simple settings. And then let’s rework it to use headers.

Let’s create a project:

Project name: P1111_PreferenceFragment
Build Target: Android 4.1
Application name: PreferenceFragment
Package name: en.startandroid.develop.p1111preferencefragment
Create Activity: MainActivity

Add rows to strings.xml:

CheckBox 1
Summary of CheckBox 1
EditText 1
Summary of EditText 1
List 1
Summary of List 1



The texts will be used to describe the settings and the arrays will be used for the list.

Let’s create a file describing the settings. To do this, in the folder really create a folder xmlIf it’s not there. And we create a file: res / xml / pref1.xml:


The three easiest settings. I will not repeat myself and describe the values โ€‹โ€‹of the elements and attributes. All this is already described in Lessons 71.

So the settings file was created. We need a snippet to show us these settings. we create a class Fragment1, next android.preference.PreferenceFragment.


package ru.startandroid.develop.p1111preferencefragment;

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

public class Fragment1 extends PreferenceFragment {

  public void onCreate(Bundle savedInstanceState) {


The addPreferencesFromResource method will read a file describing the settings and display them.

There is little to tweak MainActivity.java:

package ru.startandroid.develop.p1111preferencefragment;

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

public class MainActivity extends Activity {

  public void onCreate(Bundle savedInstanceState) {

        .replace(android.R.id.content, new Fragment1()).commit();


We do not use any layout here, but immediately add our snippet as content using the root container with ID android.R.id.content.

We all save and launch the application. We see the settings window

You can tick or click on the other two settings and get a dialog to enter a value. As usual.


Now let’s look at what the headlines give us. You can say that the headers are the root branches of the custom tree. We will make three such branches: the first two will open our snippets with settings, and the third will open the sound settings.

We already have one snippet with settings, let’s create another.

Add rows to strings.xml:

CheckBox 2
Summary of CheckBox 2
EditText 2
Summary of EditText 2
List 2
Summary of List 2
Category 1
Category 2
Screen 1
Summary of Screen 1
Screen 2
Summary of Screen 2
Header 1
Summary of Header 1
Header 2
Summary of Header 2
Header 3
Summary of Header 3

let’s create a file res / xml / pref2.xml:


Here we use the same simple elements as before, as well as categories and screens (Lesson 72). Only the screens did not specify child elements, but redirects. In the first screen, use the attribute android: fragment we indicate Fragment1We created at the beginning of the lesson. And in the second screen we indicate intent, which will open a page in the browser.

Let’s create a snippet that will show us these settings.


package ru.startandroid.develop.p1111preferencefragment;

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

public class Fragment2 extends PreferenceFragment {

  public void onCreate(Bundle savedInstanceState) {


We move on to creating headers

Create a header file res / xml / pref_head.xml:


Three headings. The first will open Fragment1, the second – Fragment2, the third, using intent, – screen settings. The icons are random.

OK. The titles are there, now you need to display them. For this we use MainActivity. But in order for it to work with headers, it must follow PreferenceActivity.

rewrite MainActivity.java:

package ru.startandroid.develop.p1111preferencefragment;

import java.util.List;

import android.preference.PreferenceActivity;

public class MainActivity extends PreferenceActivity {

  public void onBuildHeaders(List
target) { loadHeadersFromResource(R.xml.pref_head, target); } }

The onBuildHeaders method is called by the system when it is necessary to build headers. He accepts a List entry

Which we need to fill. To do this, call the loadHeadersFromResource method, and pass it our header file and populate the list.

We save and run everything. I use to run AVD, emulates the tablet by 4.1

The app looks like this:

We see three headers that we wrote down in the file pref_head.xml. First opened – Header 1, It displays Fragment1. AND Fragment1 displays settings with pref1.xml.

Click on the second heading – Header 2.

we see Fragment2 with settings from pref2.xml. You can see the categories and screens we created. Let me remind you that Screen 1 must open Fragment1, and Screen 2 – site. Let’s check.

press Screen 1.

opened Fragment1 as an attachment screen. The full nesting path is shown above. You can return by pressing the Back button. This way, you can create attachment screens with no headers.

clicking on Screen 2 will launch the browser and open the link.

clicking on Header 3 will open the screen settings.

Running the same application on your Android 3.2 tablet will look like this:

If you run it, not on a tablet, but on a smartphone with 4.1, the application itself somehow decides that the split screen into two parts will not work here and shows the headlines on one screen

and content on another

To programmatically determine whether the screen will be split into two or not, you can use the isMultiPane method.

In the next lesson:

– programmatically place elements in ActionBar
– we use elements from fragments

Discuss in the forum [39 replies]

Leave a Comment