RSS

Android Preferences

04 Jul

Hey, I checked on the preferences in android. A blog post ago I made a menu that would allow for someone to set his/her preferences.One cool thing about Preferences is that whatever values stored in them will not be lost even after exiting the app. So values like the persons name and if the user was fed up with our music and set it off and other stuff are always good to remember each time the user starts the app.

First off, I created a new folder to keep all the preferences. Its worth noting that,  though prefs.xml is an  xml file but this is will contain Preferences as opposed to the usual ones containing the layout. Created the prefs.xml, or whatever you want to  name it. And in the prefs.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
	<EditTextPreference 
	    android:title="Name"
	    android:key="name"
	    android:summary="Enter your name: "
	    />
	
	<CheckBoxPreference 
	    android:title="Sound"
	    android:defaultValue="true"
	    android:key="sound"
	    android:summary="Check to have sound"
	    />
	
	<ListPreference 
	    android:title="Options"
	    android:key="options"
	    android:summary="Choose one of the options"
	    android:entries="@array/list"
	    android:entryValues="@array/values"
	    />
    
</PreferenceScreen>

The ListPreference gets values from an array set up in an xml file called array in the values folder. The array.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
	<EditTextPreference 
	    android:title="Name"
	    android:key="name"
	    android:summary="Enter your name: "
	    />
	
	<CheckBoxPreference 
	    android:title="Sound"
	    android:defaultValue="true"
	    android:key="sound"
	    android:summary="Check to have sound"
	    />
	
	<ListPreference 
	    android:title="Options"
	    android:key="options"
	    android:summary="Choose one of the options"
	    android:entries="@array/list"
	    android:entryValues="@array/values"
	    />
    
</PreferenceScreen>

And the java file for this (notice the difference as this isn’t a layout xml but preference)

package com.learning.gilo;

import android.os.Bundle;
import android.preference.PreferenceActivity;
//it extends PreferenceActivity and not Activity as we've been doing
public class Prefs extends PreferenceActivity{
	protected void onCreate(Bundle b){
		super.onCreate(b);
		//This will add the preferences from prefs.xml
		addPreferencesFromResource(R.xml.prefs);
		
	}

}

In the menu we are going to start an intent when the preference item is clicked that will start Prefs activity.

public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
		case R.id.about:
			Intent i = new Intent("com.learning.gilo.ABOUTUS");
			startActivity(i);
			
			break;
		case R.id.exit:
			finish();
			break;
		case R.id.preferences:
			//This will start the Prefs activity
			Intent p = new Intent("com.learning.gilo.PREFS");
			startActivity(p);
			break;
		
		}
		
		return false;
	}

 

 

Advertisements
 
1 Comment

Posted by on July 4, 2012 in Android, Android tutorials, Code

 

Tags:

One response to “Android Preferences

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: