1

Possible Duplicate:
how to call the ok button in the EditTextPreference

I want to validate the Inputs (enter 6 digits) of an EditTextPreference dialog box.

This is how my (relevant) preferences.xml snippet looks like :

            <!--EditTextPreference-->
            <com.app.preferences.UpdatePincodePreference
                android:key="PIN_CODE_PREFERENCE"
                android:title="@string/pincode_preference_title" 
                android:summary="@string/pincode_preference_summary"
                android:dialogTitle="@string/pincode_preference_dialog_title"
                android:dialogMessage="@string/pincode_preference_dialog_message" 
                android:inputType="number"
            />

How do I test that the user has not entered less or more than 6 digits in the EditText of the preference dialog?

Basically I need to set an onClickListener() on the OK button, but how to I get a hold of the OK button which I did not define. Its the default view of an EditTextPreference, and so is the Cancel button.

The question is exactly the same as "how to call the ok button in the EditTextPreference" but the links provided in the accepted solution are broken now.

Community
  • 1
  • 1
Vikas Singh
  • 1,781
  • 7
  • 27
  • 54

3 Answers3

2

The author of the solution has moved his project from Google Code to GitHub. You can find the new project at https://github.com/Knickedi/android-toolbox and the links to the two files he was referring to validating DialogPreference and validating EditTextPreference

Rajesh
  • 15,724
  • 7
  • 46
  • 95
  • Awesome! Thats what I was looking. Seems to be a pretty elaborate solution. Will dig out whats relevant to me. Thnx! – Vikas Singh Apr 20 '12 at 11:37
  • Cant understand whats happening in the code :(. Can you explain me just the relevant logic. All I need is to check if 6 digits were entered when the user pressed OK button in the EditTextPreference dialog box? Thnx !!! – Vikas Singh Apr 20 '12 at 11:56
2

This could be achieved using setOnPreferenceChangeListener()

public UpdatePasswordPreference(Context context, AttributeSet attrs) {


    this.setOnPreferenceChangeListener(new OnPreferenceChangeListener() 
    {   
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) 
        {
            MobicopLogger.d("Preference input changed");
            try 
            {
                if(newValue.toString().length() != 6)
                    return false;
                else
                    return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }

    });


}
Vikas Singh
  • 1,781
  • 7
  • 27
  • 54
0

create a custom layout and apply it to the preference by the following override method :

@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    super.onPrepareDialogBuilder(builder);    //To change body of overridden methods use File | Settings | File Templates.
    builder.setView(LayoutInflater.from(ctx).inflate(R.layout.custome_preference_layout,null));
}
Vikas Singh
  • 1,781
  • 7
  • 27
  • 54