Prefernces and PreferenceActivity example

In this example we shall show you how to use Preferences with Android. This functionality is used for storing user’s preferences and using them for example when the application starts.

For using the Preferences framework the following steps must be followed:

These are demonstrated in the code snippet(s) below:

package com.javacodegeeks.android.preferences;

import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;

public class QuickPrefsActivity extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  addPreferencesFromResource(R.xml.preferences);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

  menu.add(Menu.NONE, 0, 0, "Show current settings");

  return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

  switch (item.getItemId()) {

case 0:

    startActivity(new Intent(this, ShowSettingsActivity.class));

    return true;

  }

  return false;
    }

}
<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory 

  android:title="First Category"

  android:key="first_category">

  <CheckBoxPreference 

android:key="perform_updates"

android:summary="Enable or disable data updates"

android:title="Enable updates" 

android:defaultValue="true"

  />

  <ListPreference 

android:key="updates_interval"

android:title="Updates interval"

android:summary="Define how often updates will be performed"

android:defaultValue="1000" 

android:entries="@array/updateInterval"

android:entryValues="@array/updateIntervalValues"

android:dependency="perform_updates"

  />    

    </PreferenceCategory>

    <PreferenceCategory 

  android:title="Second Category"

  android:key="second_category">

  <EditTextPreference

android:key="welcome_message"

android:title="Welcome Message" 

android:summary="Define the Welcome message to be shown"

android:dialogTitle="Welcome Message"

android:dialogMessage="Provide a message"    

android:defaultValue="Default welcome message" />

    </PreferenceCategory>

</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string-array name="updateInterval">

  <item name="1000">Every 1 second</item>

  <item name="5000">Every 5 seconds</item>

  <item name="30000">Every 30 seconds</item>

  <item name="60000">Every 1 minute</item>

  <item name="300000">Every 5 minutes</item>
    </string-array>

    <string-array name="updateIntervalValues">

  <item name="1000">1000</item>

  <item name="5000">5000</item>

  <item name="30000">30000</item>

  <item name="60000">60000</item>

  <item name="300000">300000</item>
    </string-array>

</resources>
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.javacodegeeks.android.preferences"

android:versionCode="1"

android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

  <activity android:name=".QuickPrefsActivity" 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=".ShowSettingsActivity" />

    </application>

    <uses-sdk android:minSdkVersion="3" />

</manifest>
package com.javacodegeeks.android.preferences;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;

public class ShowSettingsActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.show_settings_layout);

  SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

  StringBuilder builder = new StringBuilder();

  builder.append("n" + sharedPrefs.getBoolean("perform_updates", false));
  builder.append("n" + sharedPrefs.getString("updates_interval", "-1"));
  builder.append("n" + sharedPrefs.getString("welcome_message", "NULL"));

  TextView settingsTextView = (TextView) findViewById(R.id.settings_text_view);
  settingsTextView.setText(builder.toString());

 }

}
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView

  android:id="@+id/settings_text_view"

  android:layout_width="fill_parent" 

  android:layout_height="wrap_content"
    />

</LinearLayout>

 
This was an example of how to use Preferences with Android.

Related Article:

Share and enjoy!
© 2010-2012 Examples Java Code Geeks. Licenced under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.