Android How to - Programmatically Retrieving and Modifying the Preferences Values








To make use of the preferences in your application, you use the SharedPreferences class.

The following code shows how to programmatically Retrieving and Modifying the Preferences Values.

Example

package com.java2s.myapplication3.app;
/*from   w w  w .jav a2s  .  c  o m*/
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences appPrefs =
                getSharedPreferences("com.java2s.UsingPreferences_preferences",
                        MODE_PRIVATE);
        DisplayText(appPrefs.getString("editTextPref", ""));

        appPrefs = getSharedPreferences("com.java2s.UsingPreferences_preferences",
                MODE_PRIVATE);
        SharedPreferences.Editor prefsEditor = appPrefs.edit();
        prefsEditor.putString("editTextPref", "new value");
        prefsEditor.commit();
    }

    private void DisplayText(String str) {
        Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
    }
}




Note

Here is the code to create the Preference.

Create a file at res/xml/myapppreferences.xml and populate the myapppreferences.xml file as follows:

         <?xml version="1.0" encoding="utf-8"?>
         <PreferenceScreen
             xmlns:android="http://schemas.android.com/apk/res/android">
             <PreferenceCategory android:title="Category 1">
                 <CheckBoxPreference
                     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" />
                 <RingtonePreference
                     android:summary="Select a ringtone"
                     android:title="Ringtones"
                     android:key="ringtonePref" />
                 <PreferenceScreen
                     android:title="Second Preference Screen"
                  android:summary= "Click here to go to the second Preference Screen"
                  android:key="secondPrefScreenPref" >
                  <EditTextPreference
                      android:summary="Enter a string"
                      android:title="Edit Text (second Screen)"
                      android:key="secondEditTextPref" />
              </PreferenceScreen>
          </PreferenceCategory>
      </PreferenceScreen>

Java code

      import android.os.Bundle;
      import android.preference.PreferenceActivity;
/*from  w w w.  j av a  2  s .  c om*/
      public class AppPreferenceActivity extends PreferenceActivity {
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              //load the preferences from an XML file
              addPreferencesFromResource(R.xml.myapppreferences);
          }
      }