PreferenceActivity provides a convenient way to implement system settings screens in Android applications. Instead of manually creating configuration files and handling data persistence, PreferenceActivity manages these details automatically through SharedPreferences.
First, create a XML folder in the res directory and add a configuraton file named preferences.xml.
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="auto_sync_key"
android:summary="Enable automatic synchronization"
android:summaryOff="Automatic sync disabled"
android:summaryOn="Automatic sync enabled"
android:title="Auto Sync" />
<PreferenceCategory android:title="Network Settings" >
<CheckBoxPreference
android:key="mobile_data_key"
android:summary="Allow downloads over mobile network"
android:summaryOff="Mobile data downloads disabled"
android:summaryOn="Mobile data downloads enabled"
android:title="Download via Mobile Data" />
</PreferenceCategory>
<EditTextPreference
android:dialogTitle="Enter username"
android:key="username_key"
android:negativeButtonText="Cancel"
android:positiveButtonText="Save"
android:summary="Your display name"
android:title="Display Name" />
<ListPreference
android:dialogTitle="Select theme"
android:entries="@array/theme_options"
android:entryValues="@array/theme_values"
android:key="theme_key"
android:positiveButtonText="OK"
android:summary="Application theme"
android:title="Theme" />
</PreferenceScreen>
For ListPreference, define the available options in strings.xml under the values folder.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_title">Settings Demo</string>
<string-array name="theme_options">
<item>Light</item>
<item>Dark</item>
<item>System Default</item>
</string-array>
<string-array name="theme_values">
<item>light</item>
<item>dark</item>
<item>system</item>
</string-array>
</resources>
Create a simple main layout with a button to navigate to the settings screen.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_title"
android:textSize="20sp" />
<Button
android:id="@+id/open_settings_btn"
android:onClick="navigateToSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Open Settings" />
</LinearLayout>
MainActivity.java
package com.example.settingsdemo;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String themeValue = preferences.getString("theme_key", "system");
Toast.makeText(this, "Current theme: " + themeValue, Toast.LENGTH_SHORT).show();
}
public void navigateToSettings(View view) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
}
SettingsActivity.java
package com.example.settingsdemo;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
To retrieve saved preference values throughout the application, use PreferenceManager.getDefaultSharedPreferences() with the appropriate key names defined in you're preferences XML file.