Search code examples
androidsharedpreferencespreferenceactivity

How to set the values of PreferenceActivity(R.xml.preferences) dynamically loading it from SharedPreferences?


I have a MyPreferenceActivity that extends PreferenceActivity like this :

public class MyPreferenceActivity extends PreferenceActivity{


  @Override
  public void onCreate(Bundle savedInstanceState) {     
    super.onCreate(savedInstanceState);        
    addPreferencesFromResource(R.xml.preferences);  
  }

}

I also have custom shared preferences that R.xml.preferences are tied to by individual preference classes...for e.g.

preferences.xml has

            <!--EditTextPreference-->
            <com.myapp.preferences.PrimaryNumberPreference
                android:key="PREFS_PRIMARY_NUMBER"
                android:title="@string/primary_number_preference_title" 
                android:summary="@string/primary_number_preference_summary"
                android:dialogTitle="@string/primary_number_preference_dialog_title" 
                    android:dialogMessage="@string/primary_number_preference_dialog_message"  
            />

PrimaryNumberPreference.java :

public class PrimaryNumberPreference extends EditTextPreference {

Context ctx = null;

public PrimaryNumberPreference(Context context) {
    super(context);
    this.ctx = context;
}

public PrimaryNumberPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.ctx = context;
}

public PrimaryNumberPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.ctx = context;

}

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if (positiveResult){    
        customPreferenceibrary.setPrefsPrimaryNumber(getText());
    }
}

}

OK so all seems to be in place. Now what I want is everytime the PreferenceActivity is loaded, it should fetch the current sharedpreference values that are stored in their respective key and prepopulate the elements of the PreferenceActivity...

Eg: PrimaryNumber shared preference is set to 1234 when my app is installed. Now if I go to MyPreferenceActivity and browse the PrimaryNumber EditTextPreference, the text box should be prepopulated by 1234.

Something(pseudo code) like :

MyPreferenceActivity.PrimaryNumberPreference.Val = getSharedPreferences(prefsPrimaryNumber)

How can I do that?

EDIT

Please let me know if the question is unclear and needs better explanantion. I am sure the answer is a simple implementation of something very standard in android. All PreferenceActivity elements pick the current value to be displayed from the stored SharedPreferences only, right?


Solution

  • I figured out the answer with the help of suggestions provided by @Mel and Its very easy.

    In the class that extends the corresponding PreferenceActivity element(e.g. shown below) just add this.setValue in the constructor of that preference element class. Thats it!

    public class selectColorPreference extends ListPreference{
    
        Context ctx = null;
        private String error = null;
        private int prefsRemoteSMSAccess;
        MyPreferencesLibrary myPreferencesLibrary;
    
        public selectColorPreference(Context context) {
            super(context);
            this.ctx = context;
    
            myPreferencesLibrary = new MyPreferencesLibrary(ctx);
            this.setValue(myPreferencesLibrary.getSharedPreferenceValueForColor());
    
        }
    }