4

I want to have an EditTextPreference that will disable the OK button if there is no text in the EditText field. I created a custom EditTextPreference class and I am able to get the EditText object and set a TextWatcher, but I can't find a way to disable the button. It looks like I just don't have access to the OK and Cancel buttons in the Dialog.

Anyone know a way to get at these buttons or to do what I am trying to do?

Only other option is to try to create from scratch a custom Dialog that looks like and mimics the EditTextPreference.

Bob
  • 891
  • 1
  • 9
  • 14

2 Answers2

9

Here is a code sample that enables/disables button depending on whether onCheckValue function returns true or false.

public class ValidatedEditTextPreference extends EditTextPreference
{
    public ValidatedEditTextPreference(Context ctx, AttributeSet attrs, int defStyle)
    {
        super(ctx, attrs, defStyle);        
    }

    public ValidatedEditTextPreference(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);                
    }

    private class EditTextWatcher implements TextWatcher
    {    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count){}

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

        @Override
        public void afterTextChanged(Editable s)
        {        
            onEditTextChanged();
        }
    }
    EditTextWatcher m_watcher = new EditTextWatcher();

    /**
     * Return true in order to enable positive button or false to disable it.
     */
    protected boolean onCheckValue(String value)
    {        
        return Strings.hasValue(value);
    }

    protected void onEditTextChanged()
    {
        boolean enable = onCheckValue(getEditText().getText().toString());
        Dialog dlg = getDialog();
        if(dlg instanceof AlertDialog)
        {
            AlertDialog alertDlg = (AlertDialog)dlg;
            Button btn = alertDlg.getButton(AlertDialog.BUTTON_POSITIVE);
            btn.setEnabled(enable);                
        }
    }

    @Override
    protected void showDialog(Bundle state)
    {
        super.showDialog(state);

        getEditText().removeTextChangedListener(m_watcher);
        getEditText().addTextChangedListener(m_watcher);
        onEditTextChanged();
    }    
}
inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • still don't see how I can access the buttons. I need to be able to disable the OK button. What you are suggesting is just make my own dialog. If I take that approach I will just build a custom Dialog class. – Bob Jan 21 '11 at 19:46
  • I've updated the answer. I've checked this new approach myself, and it works for me. – inazaruk Feb 08 '11 at 22:35
2

need change the old EditTextPreference to the new ValidatedEditTextPreference in preference.xml.

I did the following:

Old code:

<EditTextPreference
    android:dialogMessage="Please input your email"
    android:dialogTitle="Set email"
    android:key="Email"
    android:summary="Your email"
    android:title="-Missed call send to" android:defaultValue="xxx@xxx.xxx"/>

New code:

<com.tanggod.missedcall2mail.ValidatedEditTextPreference
    android:dialogMessage="Please input your email"
    android:dialogTitle="Set email"
    android:key="Email"
    android:summary="Your email"
    android:title="-Missed call send to" android:defaultValue="xxx@xxx.xxx"/>
DàChún
  • 4,751
  • 1
  • 36
  • 39
  • 1
    Why does he *need* to change to the *new* `ValidatedEditTextPreference`? Apparently, it's not part of the API because you need to add some other library to make it work. Also, you're answering to something that was asked 2 years ago, so Bob may not specifically need an answer to this issue. – BLaZuRE Jul 22 '13 at 10:29
  • Yes, you are right, I just find that it is 2 years ago. Because I modified EditTextPreference to ValidatedEditTextPreference as inazaruk's way. I just demonstrate how to use it. – DàChún Jul 22 '13 at 11:15