androidandroid-edittextpreferences

Open EditTextPreference through code (programmatically)


I have made the EditTextPreference 'textPasscode' dependant on a CheckBoxPreference 'checkBoxPasscode'. I want the 'textPasscode' to open up as soon as the user checks the check box.. Is it even possible? If it is, what can I use in the onSharedPreferenceChanged() function?

public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    if(key.contentEquals("checkBoxPasscode")){
       // ----some method to open edit text "textPasscode" ??
    }
}

Solution

  • This problem was very annoying to me, so after implementing Sandor suggestion, i've searched for better solution in the Android Reference, and look what i've found.
    EditTextPreference inherits from DialogPreference and this class have the showDialog method, so i've made a new class from EditTextPreference with a show method, and it works like a charme.

    Here is some code:

    public class MyEditTextPref extends EditTextPreference {
        //...constructor here....
    
        public void show() {
            showDialog(null);
        }
    }
    

    In my settings.xml (wich i use to generate the ActivitySettings Layout) i've added myEditTextPref

    <package.that.contains.MyEditTextPreferences 
        android:key="myPref"
        android:title="@string/pref_title"
        android:summary="@string/pref_summary"
        android:dialogTitle="@string/dialog_title"
        android:dialogMessage="@string/dialog_message"
    />
    

    And the last thing i've done is the onSharedPreferenceChanged method in the PreferenceActivity

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equalsIgnoreCase(MY_CHECK_BOX)) {
            MyEditTextPreferences myPref = (MyEditTextPreferences) findPreference("myPref");
            myPref.show();
        }
    }
    

    ps.: actually i'm not using PreferenceFragment because i want pre-honeycomb compatibility but i don't think this code change much.