Here is source code of the Program to Demonstrate Preference Headers in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.
In rare cases, you might want to design your settings such that the first screen displays only a list of subscreens. When you’re developing such a design for Android 3.0 and higher, you should use a new “headers” feature in Android 3.0, instead of building subscreens with nested PreferenceScreen elements.
To build your settings with headers, you need to:
1.Separate each group of settings into separate instances of PreferenceFragment. That is, each group of settings needs a separate XML file.
2.Create an XML headers file that lists each settings group and declares which fragment contains the corresponding list of settings.
3.Extend the PreferenceActivity class to host your settings.
4.Implement the onBuildHeaders() callback to specify the headers file.
A great benefit to using this design is that PreferenceActivity automatically presents the two-pane layout when running on large screens.
for more info go to:-
http://developer.android.com/guide/topics/ui/settings.html#top
MainActivity.java
package com.example.prefernceheaders; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button but2 = (Button) findViewById(R.id.button1); but2.setOnClickListener(new View.OnClickListener() { Intent in = new Intent("com.example.prefernceheaders.Pref"); @Override public void onClick(View v) { // TODO Auto-generated method stub startActivity(in); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
Pref.java
package com.example.prefernceheaders; import java.util.List; import android.preference.PreferenceActivity; public class Pref extends PreferenceActivity { @Override public void onBuildHeaders(List<Header> target) { loadHeadersFromResource(R.xml.preference_headers, target); } }
Activity_main.xml
<RelativeLayout 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" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_marginBottom="190dp" android:text="LAUNCH" /> </RelativeLayout>
Include both pref.xml and pref_headers in /res/xml folder.
pref.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="...." > <PreferenceScreen android:key="button_voicemail_category_key" android:persistent="false" android:title="voicemail" > <ListPreference android:key="button_voicemail_provider_key" android:title="voicemail_provider" /> <!-- opens another nested subscreen --> <PreferenceScreen android:key="button_voicemail_setting_key" android:persistent="false" android:title="voicemail_settings" > ... </PreferenceScreen> <RingtonePreference android:key="button_voicemail_ringtone_key" android:ringtoneType="notification" android:title="voicemail_ringtone_title" /> ... <Preference android:title="prefs_web_page" > <intent android:action="android.intent.action.VIEW" android:data="http://www.google.com" /> </Preference> </PreferenceScreen> </PreferenceScreen>
pref_headers.xml
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> <header android:fragment="com.example.prefs.SettingsFragmentOne" android:title="prefs_category_one" android:summary="prefs_summ_category_one" /> <header android:fragment="com.example.prefs.SettingsFragmentTwo" android:title="prefs_category_two" android:summary="prefs_summ_category_two" /> </preference-headers>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.prefernceheaders" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.prefernceheaders.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.prefernceheaders.Pref" android:theme="@android:style/Theme.Black.NoTitleBar" > <intent-filter> <action android:name="com.example.prefernceheaders.Pref" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>