Home » Android » core » preference » Prefernces and PreferenceActivity example

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

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:

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of