Preference Activity

PreferenceActivity on android

Search Google for: PreferenceActivity on android

Search Android Developers Group for: PreferenceActivity on android

Search Android Beginers Group for: PreferenceActivity on android

Search Google Code for: PreferenceActivity on android

Search Android Issues Database for: PreferenceActivity on android

SDK Reference

Types of Controls in Preferences


<CheckBoxPreference android:key="audio_preference" 
android:title="Turn on/off Sound" 
android:summary="Use this to turn on and off the sound"></CheckBoxPreference>

<ListPreference android:summary="Select the time window for a game" 
                 android:dialogTitle="Choose One From Below" 
                 android:key="gametime_preference" 
                 android:title="Game Time"
                 android:entries="@array/timewindows"
                 android:entryValues="@array/timewindows_values"/>

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class GameSettingsActivity extends PreferenceActivity{

   /**
    * First method to be called when activity is created. 
    * This method will do all the initial setups and initializations.
    * 
    */
   public void onCreate(Bundle savedInstanceState) {
        
      super.onCreate(savedInstanceState);
        
      /**
       * Setting the view
       */
      addPreferencesFromResource(R.xml.game_preferences);
        
      
    }
}

How do i default preferences?

Defaulting Preferences in Android Preference Activity

Search for: Defaulting Preferences in Android Preference Activity

Initializing the default values from the Preference Xml


<ListPreference android:summary="Select the time window for a game" 
                 android:dialogTitle="Choose One From Below" 
                 android:key="gametime_preference" 
                 android:title="Game Time"
                 android:entries="@array/timewindows"
                 android:entryValues="@array/timewindows_values"
                 android:defaultValue="1"/>

<ListPreference android:summary="Select the time window for a game" 
                 android:dialogTitle="Choose One From Below" 
                 android:key="gametime_preference" 
                 android:title="Game Time"
                 android:entries="@array/timewindows"
                 android:entryValues="@array/timewindows_values"
                 android:defaultValue="1"/>

This is what i know i defined two arrays one for the entries and another for the entry values. Both were of type arrays.

But its not working. List preference in giving a null pointer exception when defaulting to a value.

Sample as below


   <array name="timewindows">
      <item>2 Minutes</item>
      <item>3 Minutes</item>
      <item>5 Minutes</item>
   </array>   
   
   <array name="timewindows_values">
      <item>1</item>
      <item>2</item>
      <item>3</item>
   </array>

I have to change the array to string-array and this solves it. Please remember this is a common issue everyone will face.

Changed the code as below and all my issues were solved..Thanks to string-array.


   <string-array name="timewindows">
      <item>2 Minutes</item>
      <item>3 Minutes</item>
      <item>5 Minutes</item>
   </string-array>   
   
   <string-array name="timewindows_values">
      <item>1</item>
      <item>2</item>
      <item>3</item>
   </string-array>

Well How do i change the preference screen background?

Yes we can use theming to change the background. Default background is always better as it matched all other android preferences.

But let me still try to change the background using theming.