samedi 3 février 2018

How to simulate the Android enter password dialog box with 'show password' option

I have been able to create a layout in xml for an Alert Dialog with header text, an edittext (for the password) and a checkbox for 'show password'. I am able to retrieve the password when the dialog is closed. What I have been unable to do is to respond clicking on the checkbox.

I have tried numerous attempts using 'Builder.setMultiChoiceItems' but using that approach adds its OWN checkbox in addition to the one I have in my layout. Also, sticks it under the title ignoring my layout. I don't want it there! The handler responds to clicks on the rogue checkbox, but not my checkbox.

So how can I use the Builder.setMultiChoiceItems approach and use the checkbox in my layout? Or if that is not possible, how can I control where Builder.setMultiChoiceItems places the checkbox?

Is there another means of responding to clicks on my checkbox without killing the dialog box? (I can read the state of my checkbox when I exit the dialog but that is not what I want!)

My layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:padding="24dp">

    <android.support.v7.widget.AppCompatTextView
        style="@style/Base.TextAppearance.AppCompat.Subhead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="24dp"
        android:text="@string/enter_password" />

    <EditText
        android:id="@+id/edit_network_password"
        style="@style/Widget.AppCompat.EditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:inputType="textPassword"
        android:hint="@string/network_password"
        android:textColorHint="#ffff25e6"/>

    <android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/do_not_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/show_password" />

</LinearLayout>

My code

    LayoutInflater inflater = getLayoutInflater();
    final View dialogContent = inflater.inflate(R.layout.dialog_password_notification, null);
    final String[] items = {getString(R.string.show_password)};

    android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
    builder.setTitle(R.string.password_dialog_title);
    final EditText edit = dialogContent.findViewById(R.id.edit_network_password);
    builder.setView(dialogContent);
    builder.setMultiChoiceItems(items, null,
            new DialogInterface.OnMultiChoiceClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int selectedItemId,
                                    boolean isSelected)
                {
                    Log.i(TAG, "Got something!");
                    if (selectedItemId == R.id.do_not_show)
                    {
                        if(isSelected)
                        {
                            Log.i(TAG, "Check box Show password");
                            // Show the password
                        }
                        else
                        {
                            // Hide the password
                            Log.i(TAG, "Check box Hide password");
                        }

                    }
                }
            });
    builder.setPositiveButton("Done!", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                LniHubAssistantActivity.selectedWifi.setPasskey(edit.getText().toString());
                Prefs.saveToSharedPreference(context, Prefs.WIFI_KEY, LniHubAssistantActivity.selectedWifi.getPasskey());
                //Your logic when OK button is clicked
                finish();
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
            }
        });
    android.support.v7.app.AlertDialog dialog = builder.create();
    dialog.show();




Aucun commentaire:

Enregistrer un commentaire