Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set null validation in edittextpreference dialog

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

like image 950
Rishabh Srivastava Avatar asked Aug 19 '13 06:08

Rishabh Srivastava


2 Answers

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();
like image 39
Philipp Jahoda Avatar answered Sep 29 '22 18:09

Philipp Jahoda


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.

like image 187
Rishabh Srivastava Avatar answered Sep 29 '22 18:09

Rishabh Srivastava