4

I am trying to change the positiveButtonText of Dialog in the `EditTextPreference. I have tried the following with no success :

    // The reference to the preference in the view heirarchy
    mUrlPreference = (EditTextPreference) getPreferenceScreen().findPreference(pref);       
    // Add a textwatcher
    mUrlPreference.getEditText().addTextChangedListener(prefWatcher);

The prefWatcher is the following :

private TextWatcher prefWatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        Log.i(TAG_LOG, "Updating the positive button text");
        mUrlPreference.setPositiveButtonText("Download");
    }
};

When I change the text in the EditText, the Log gets printed, but the Positive button text is not changing. Is there anything I'm missing here? My initial thought was that I would have to refresh the dialog's view hierarchy, but I don't see any methods in the API to do it. Any ideas as to what I'm missing here?

500865
  • 6,920
  • 7
  • 44
  • 87

1 Answers1

1

As per the documentation of setPositiveButtonText(), the updated text will be shown on subsequent dialogs. So to actually affect the button, do this instead:

@Override
public void afterTextChanged(Editable s) {
    mUrlPreference.setPositiveButtonText("Download");

    Button button = (Button) mUrlPreference.getDialog().findViewById(android.R.id.button1);
    button.setText("Download");
    button.invalidate(); // if necessary
}
Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
  • Thanks!! But how did you get the id of the positive button? – 500865 Mar 19 '12 at 17:57
  • @500865 BUTTON_POSITIVE was originally known as [BUTTON1](http://developer.android.com/reference/android/content/DialogInterface.html#BUTTON1). The id is mentioned [here](http://developer.android.com/reference/android/R.id.html#button1). – Dheeraj Vepakomma Mar 19 '12 at 18:01