Here is source code of the Program to Demonstrate Prefernce Fragement in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.
MainActivity.java
package com.example.preferncefragement; 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 but = (Button) findViewById(R.id.Launch); final Intent intent = new Intent( "com.example.preferencefragement.PrefActivity"); but.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startActivity(intent); } }); } @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; } }
PrefFragement.java
package com.example.preferncefragement; import android.os.Bundle; import android.preference.PreferenceFragment; public class PrefFragement extends PreferenceFragment{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.prefs); } }
PrefActivity.java
package com.example.preferncefragement; import android.os.Bundle; import android.preference.PreferenceActivity; public class PrefActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); getFragmentManager().beginTransaction() .replace(android.R.id.content, new PrefFragement()).commit(); } }
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" android:background="@android:color/darker_gray"> <Button android:id="@+id/Launch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@android:color/white" android:text="LAUNCH Preference Activity" /> </RelativeLayout>
prefs.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:title="...." > <PreferenceCategory android:summary="Preference Category Screen 1" android:title="Your Title Goes here" > <EditTextPreference android:key="Edit Text" android:summary="This is an Edit Text" android:title="Edit Text" /> <EditTextPreference android:key="Edit Text2" android:summary="Another Edit Text" android:title="Edit Text" /> </PreferenceCategory> <PreferenceCategory android:summary="Preference Category Screen 2" android:title="Your Title Goes here" > <CheckBoxPreference android:key="checkBox" android:summary="A check box " android:title="Keep me logged in" /> <ListPreference android:entries="@array/array1" android:entryValues="@array/array2" android:key="listpref" android:summary="List preference example" android:title="List preference" /> </PreferenceCategory> </PreferenceScreen>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.preferncefragement" 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.preferncefragement.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.preferencefragement.PrefActivity" android:theme="@android:style/Theme.Black.NoTitleBar" > <intent-filter> <action android:name="com.example.preferencefragement.PrefActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>