Question

How to set null validation in edittextpreference dialog so that if it is null, the user should not be able to click ok and some message should be displayed in the dialog itself. I have been trying to find it for a long time but no success....

Was it helpful?

Solution 3

When edittext is null then ok button will be disabled and as soon as the text is entered it will be enabled::

public class CustomEditTextPreference extends EditTextPreference implements
        OnClickListener {


        public CustomEditTextPreference(Context ctx, AttributeSet attrs, int defStyle)
        {
            super(ctx, attrs, defStyle);        
        }

        public CustomEditTextPreference(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)
        {        
            if (value.trim().equals(""))
            {
                return false;
            }
            return true;
        }

        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();
        }    
    }

OTHER TIPS

edttxtpref = (EditTextPreference) getPreferenceScreen().findPreference(
            "edttxtkey");
    edttxtpref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(
                android.preference.Preference preference, Object newValue) {
            if (newValue.toString().trim().equals("")) {

                Toast.makeText(getActivity(), "Username can not be empty",
                        Toast.LENGTH_LONG).show();

                return false;
            }
            return true;
        }
    });

This way the validation is done and if we want to display the message in dialog itself then a custom dialog has to be created as already told by Phil.

I think what you are looking for is this. You are using the predefined PreferenceDialog (with EditText) and want to check if the Text is null. According to my knowledge, the "text" in this case is the changedPreference, therefore you can go with this:

Simply use an onPreferenceChangedListener for that.

yourPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
         @Override
         public boolean onPreferenceChange(Preference preference, Object changedValue) {

              if(changedValue == null) {
                    // handle it being null

                    return false;
              } else {

                    return true;
              }
         }
   });

For more advanced requirements, I would recommend that you implement your own Dialog and inside it, do whatever you desire. You can make that happen by defining a Preference list entry in .xml and then spawn the Dialog upon clicking on it.

Preference yourCustomPref = (Preference) findPreference("yourPref");
yourCustomPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {

            // spawn your dialog here
            return true;
        }
    });

This is how you could implement your custom EditText Dialog:

public Builder buildDialog(final Context c) {

        AlertDialog.Builder builder = new AlertDialog.Builder(c);
        builder.setTitle("EditText Dialog");
        builder.setMessage("Enter text:");

        LinearLayout llV = new LinearLayout(c);
        llV.setOrientation(1); // 1 = vertical

        final EditText patName = new EditText(c);
        patName.setHint("Enter text...");

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f);   
        lp.bottomMargin = 20;
        lp.rightMargin = 30;
        lp.leftMargin = 15;

        patName.setLayoutParams(lp);

        llV.addView(patName);

        builder.setView(llV);

        builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                if(patName.getText().toString().length() > 0) {

                } else {

                }       
            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });

        return builder;
    }

And then call it like this:

buildDialog(yourcontext).show();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top