PreferenceActivity
-
You may want your application to provide preferences the users get to set.
-
Example: save login information, how often the app updates external (web) information,
or user may specify a default font size for viewing, etc. - You can use the android SharedPreferences object to same application data like user preferences. (we will explore this more in our module on data)
OPTION 1: Use PreferenceActivity (an Acitivity) class to dispaly activity for user to edit preference THIS IS EXPLAINED HERE
OPTION 2: Since Android 3.0 you can use PreferenceFragment
What is PreferenceActivity?
- a special type of Activity that displays a hierarchy of preferences to the user.
Option 1: PreferenceActivity --an Example
- specify the preferences you want to offer in app to user in xml file myapppreferences.xml
- create your PreferenceActivity class that extends PreferenceActivity and use the addPreferencesFromResource(R.xml.myapppreferences) method to load the preferences from res/xml/myapppreferences.xml
- You will create an applicaiton that has a series of buttons and text fields that let you load preferences screen, display the values or modify them
HERE ARE THE PREFERENCES you are specifying the app has: myapppreferences.xml in res/xml folder
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Category 1"> <ChekBoxPreference android:title="Checkbox" android:defaultValue="false" android:summary="True or False" android:key="checkboxPref" /> </PreferenceCategory> <PreferenceCategory android:title="Category 2"> <EditTextPreference android:summary="Enter a string" android:defaultValue="[Enter a string here]" android:title="Edit Text" android:key="editTextPref" this lets you reference in your code this item /> <RingtonePreference android:summary="Select a ringtone" android:title="Ringtones" android:key="ringtonePref" this lets you reference in your code this item /> <PreferenceScreen android:title="Second Preference Screen" android:summary= "Click here to go to the second Preference Screen" android:key="secondPrefScreenPref" > this lets you reference in your code this item <EditTextPreference android:summary="Enter a string" android:title="Edit Text (second Screen)" android:key="secondEditTextPref"this lets you reference in your code this item /> </PreferenceScreen> </PreferenceCategory> </PreferenceScreen>
HERE is the App layout : main.xml in res/layout folder
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<Button android:id="@+id/btnPreferences" android:text="Load Preferences Screen" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClickLoad"/>
<Button android:id="@+id/btnDisplayValues" android:text="Display Preferences Values" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClickDisplay"/> <EditText android:id="@+id/txtString" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnModifyValues" android:text="Modify Preferences Values" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClickModify"/> </LinearLayout>HERE is the UsingPreferencesActivity.java : this uses main.xml as interface this class starts the PreferenceActivity class instance
ALSO, this class gets the currently stored preferences associated with SharedPreferences object of this APP
ALSO, this class modifies/stores the preferences the user selected in the SharedPreferences object of this APP
//event handler when load button hit it will create instance of AppPreferenceActivity public void onClickLoad(View v) { Intent i = new Intent("AppPreferenceActivity"); startActivity(i); } //method to display preferences -- uses the SharedPreferences object of this APP to retrieve previously stored
public void onClickDisplay(View view) { /* another way SharedPreferences appPrefs = getSharedPreferences("UsingPreferences_preferences", MODE_PRIVATE); */ SharedPreferences appPrefs = getSharedPreferences("appPreferences", MODE_PRIVATE); //associate storage with name "appPreferences"
DisplayText(appPrefs.getString("editTextPref", "")); } //method to store preferences -- uses the SharedPreferences object of this APP to retrieve previously stored
public void onClickModify(View view) {
/* another way
SharedPreferences appPrefs =
getSharedPreferences("net.learn2develop.UsingPreferences_preferences",
MODE_PRIVATE);
*/
SharedPreferences appPrefs = getSharedPreferences("appPreferences", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = appPrefs.edit();
prefsEditor.putString("editTextPref", ((EditText) findViewById(R.id.txtString)).getText().toString());
prefsEditor.commit();
} public void DisplayText(String s) { Toast.makeText(getBaseCOntext(), s, Toast.LENGTH_LONG).show(); }HERE is the AppPreferenceActivity : THIS IS THE PreferenceActivity class
import android.os.Bundle; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; public class AppPreferenceActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreferenceManager prefMgr = getPreferenceManager(); // this grabs the apps PreferenceManger prefMgr.setSharedPreferencesName("appPreferences"); // this sets the name associated with these prefrences as "appPreferences" //---load the preferences from an XML file--- addPreferencesFromResource(R.xml.myapppreferences); //this loads the preferences and populates interface correspondingly } }