How to set null validation in edittextpreference dialog
Asked Answered
J

3

5

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....

Judejudea answered 19/8, 2013 at 6:40 Comment(4)
thats what am asking...how to do it as i am not getting anything on this.Am not asking you to write a code for me but to tell what to do.Judejudea
@weblado.at :: edittext and edittextpreference are two different things if you know.I know how to set a validation on edittext.....and you just voted me down without even reading the questn properly.Judejudea
I think you din't read the last line....i am trying to find it since a long time but no succes....i have already tried the link you gave.the problem is that this setonpreferencechangelistener is not called.Judejudea
let us continue this discussion in chatHesketh
J
2

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();
        }    
    }
Judejudea answered 21/8, 2013 at 7:11 Comment(0)
J
6
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.

Judejudea answered 19/8, 2013 at 13:21 Comment(0)
B
2

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();
Boatyard answered 19/8, 2013 at 6:47 Comment(5)
Well then I would recommend that you implement the dialog yourself. I should only take a few minutes to achieve everything you want.Boatyard
ya that i can do but for settings(developer.android.com/guide/topics/ui/settings.html) the dialogs are inbuilt.Judejudea
Of course. But when clicking on a List element inside a PreferenceActivity you can do whatever you want. For example show your custom dialog and do all the things you want in there.Boatyard
I added an example of how to make a dialog.Boatyard
the point is not creating a custom dialog...it is adding this custom dialog to preference.Judejudea
J
2

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();
        }    
    }
Judejudea answered 21/8, 2013 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.