0

I have setup a custom Preference that must launch a dialog with 3 TextViews to confirm and change a password.

I hope that the dialog will not be closed and display a prompt information if a user input error information and click "Save" button, how can I do? Thanks!

BTW, is it suitable to use DialogPreference control to deal with the business logic in PreferenceScreen ?

public class DialogChangePassword extends DialogPreference {

    private String strPass1;
    private String strPass2;

    public DialogChangePassword(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.unlock_custom_dialog);
    }

    private View view;
    @Override
    protected View onCreateDialogView() {
        // TODO Auto-generated method stub
        view=super.onCreateDialogView(); 
        return view;
    }


    @Override
    protected void onDialogClosed(boolean positiveResult) {

         final EditText password1    = (EditText) view.findViewById(R.id.EditText_Pwd1);
         final EditText password2    = (EditText)view.findViewById(R.id.EditText_Pwd2);

         strPass1 = password1.getText().toString();
         strPass2 = password2.getText().toString();

         if (strPass1.equals(strPass2)) {
             SavePassword();
             super.onDialogClosed(positiveResult);
         } else {
             Toast.makeText(view.getContext(),"Input Error",Toast.LENGTH_LONG).show();             
             //Prevent to close the dialog! How to do?
         }    
    }
}

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="AppPreference"
    android:summary="@string/PreferenceSummary"
    android:title="@string/Preference" >

     <ui.custom.DialogChangePassword
            android:key="prefKeyResetQuests"
            android:dialogIcon="@android:drawable/ic_dialog_alert"
            android:title="Reset Password"
            android:summary="Reset Password"
            android:dialogMessage="Reset Password"
            android:positiveButtonText="Save"
            android:negativeButtonText="Cancel"/>

      />     

</PreferenceScreen>

unlock_custom_dialog.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/root"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/TextView_Pwd1"
        android:text="string/settings_oldpassword"
        android:textStyle="bold" />

    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/EditText_OldPwd" />

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/TextView_Pwd11"
        android:text="string/settings_password"
        android:textStyle="bold" />

    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/EditText_Pwd1"
        android:inputType="textPassword" />

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/TextView_Pwd2"
        android:text="string/settings_password2"
        android:textStyle="bold" />

    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/EditText_Pwd2"
        android:inputType="textPassword" />    
</LinearLayout>
HelloCW
  • 843
  • 22
  • 125
  • 310
  • App crashes? There must be a logcat. – Sufian Feb 20 '15 at 10:04
  • No crashes, I only wish the dialog don't close if a user input error infoamtion. – HelloCW Feb 20 '15 at 10:15
  • possible duplicate of [How to prevent a dialog from closing when a button is clicked](http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked) – Sufian Feb 20 '15 at 10:18

1 Answers1

1

Use this inside your DialogPreference :

@Override
public void onClick(DialogInterface dialog, int which)
{
    // If the user clicked "Ok" but the "savePassword" function returns false (mainly because the checks weren't good), we block the closing of the Dialog
    if (which == DialogInterface.BUTTON_POSITIVE && !savePassword())
    {
        try
        {
            Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
            field.setAccessible(true);
            field.set(dialog, false);
        }
        catch (NoSuchFieldException | IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
    // else, we remove the block if it was put before
    else
    {
        try
        {
            Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
            field.setAccessible(true);
            field.set(dialog, true);
        }
        catch (NoSuchFieldException | IllegalAccessException e)
        {
            e.printStackTrace();
        }
    }
}

This way, if the user want to leave the dialog before or after a failed validation, he still can. But the window won't close if the input is wrong and the user clicked "Ok".

PS : If you want to extendDialogPreference, you'll need this :

@Override
    protected void onDialogClosed(boolean exitedViaOk)
    {
        super.onDialogClosed(exitedViaOk);

        // Save only if the user clicked the "Ok" button
        if (exitedViaOk)
        {
            savePassword();
        }
    }
Nino DELCEY
  • 652
  • 1
  • 9
  • 25