Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" ??
    }
}
like image 318
Ayrton Senna Avatar asked Jun 18 '11 14:06

Ayrton Senna


2 Answers

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.

like image 116
ilmarcodacol Avatar answered Oct 22 '22 07:10

ilmarcodacol


I ran into the same problem. I wonder that it's not a common issue there are so few search result regarding this on the net.

It appears that it's not possible to show EditTextPreference manually from code, although there is an obvious workaround.

You can achieve the same appearance and behavior by creating an alert dialog and handling the onclickevent of the OK button.

This is simple general code for an text input dialog:

public static EditText showInputDialog(Context context, OnClickListener clickListener, String message)
{
    LayoutInflater factory = LayoutInflater.from(context);
    final View textEntryView = factory.inflate(R.layout.dialogedittext, null);              
    final EditText editText = (EditText)textEntryView.findViewById(id.dialogEditText);
    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder     
    .setTitle(message)
    .setView(textEntryView)
    .setPositiveButton("OK", clickListener)
    .setNegativeButton("Cancel", null).show();
    return editText;
}   

Just pass in an OnClickListener and handle the preference settings there. Don't forget to set an EditText variable to receive the input string the user entered.

I hope it helps to save some time to people who search for this issue.

like image 4
Sandor Avatar answered Oct 22 '22 05:10

Sandor