I'm doing an Android application using ActionBarSherlock
.
My problem is for making preference screen. The onBuildHeader()
method on the SherlockPreferenceActivity
class is nerver called.
This is my code
preference_headers.xml
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >
<header
android:fragment="package.fragments.MyPreferenceFragment"
android:icon="@drawable/bulle"
android:summary="An example of some preferences."
android:title="title" />
</preference-headers>
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/options" >
<PreferenceCategory>
<SwitchPreference
android:id="@+id/cb_notif"
android:defaultValue="false"
android:key="com.vincent.HTC_Dev.pref.notif"
android:summaryOff="@string/notifications_off"
android:summaryOn="@string/notifications_on"
android:title="@string/notifications" />
</PreferenceCategory>
</PreferenceScreen>
MyPreferenceActivity
public class MyPreferenceActivity extends SherlockPreferenceActivity {
@Override
public void onBuildHeaders(List<Header> target) {
super.onBuildHeaders(target);
loadHeadersFromResource(R.xml.preference_headers, target);
}
}
MyPreferenceFragment
public class MyPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
}
}
onBuildHeaders()
will only be called if you are running on API Level 11 or higher. On older devices, you are back to the classic addPreferencesFromResource()
approach.
See this sample project for an example of supporting PreferenceFragment
on API Level 11+ and classic preferences on older versions of Android.
Specifically, its SherlockFragmentActivity
looks like:
package com.commonsware.android.preffragsbc;
import java.util.List;
import android.os.Build;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockPreferenceActivity;
public class EditPreferences extends SherlockPreferenceActivity {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.HONEYCOMB) {
addPreferencesFromResource(R.xml.preferences);
addPreferencesFromResource(R.xml.preferences2);
}
}
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}
}
In onCreate()
we need to only do addPreferencesFromResource()
if we are on an older device. In this case, my sample has two preference headers, and therefore two preference XML files, so we call addPreferencesFromResource()
on both of them. On newer devices, onCreate()
does nothing but chain to the superclass, which will trigger the call to onBuildHeaders()
.
onBuildHeaders() will only be called if you are running on API Level 11 or higher. See github.com/commonsguy/cw-omnibus/tree/master/Prefs/FragmentsBC for an example of supporting PreferenceFragment on API Level 11+ and classic preferences on older versions of Android.
– CommonsWare Jun 7 '12 at 11:06
@CommonsWare can you please post this as you answer, so I can delete this copy of your answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With