Q: Implement onClick inside an OnShowListener AlertDialog kotlin I am trying to replicate a click event inside an OnShowListener of an AlertDialog that works well in Java to prevent the AlertDialog from closing when the BUTTON_POSITIVE is pressed this is my code in Java:
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    View view = getLayoutInflater().inflate(R.layout.dialog_password_confirm, null);
    builder.setView(view);
    AlertDialog dialog = builder.setPositiveButton(getString(R.string.text_ok)+"", null)
            .create();
    dialog.setTitle("Acceso Supervisor");
    dialog.setCancelable(false);
    final EditText editPass = view.findViewById(R.id.edit_password_passwordconfirm);
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(final DialogInterface dialog) {
            Button btnAceptar = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
            btnAceptar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String pass = editPass.getText().toString().trim();
                    if (pass == null || pass.equals("")){
                        Toast.makeText(getContext(), "Ingresa una contraseña valida", Toast.LENGTH_SHORT).show();
                    } else {
                        if (UsuarioModel.verifySupervisorPassword(getContext(), pass)){
                            dialog.dismiss();
                            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(editPass.getWindowToken(), 0);
                            Intent intent = new Intent(getContext(), ResumenDocActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(getContext(), "Oops! contraseña incorrecta", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
        }
    });
    dialog.setButton(DialogInterface.BUTTON_NEUTRAL,getString(R.string.text_cancel) + "", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(editPass.getWindowToken(), 0);
                }
            });
    dialog.show();
    editPass.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            editPass.post(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager inputMethodManager= (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.showSoftInput(editPass, InputMethodManager.SHOW_IMPLICIT);
                }
            });
        }
    });
    editPass.requestFocus();

This code in Java works fine it doesn't close the AlertDialog even if they enter wrong password, but failed to replicate it in Kotlin, this is my code:
val builder: AlertDialog.Builder = AlertDialog.Builder(context!!)
    val view = layoutInflater.inflate(R.layout.dialog_password_confirm, null)
    var editPass = view.findViewById<EditText>(R.id.edit_password_passwordconfirm);
    val dialog: AlertDialog = builder.setView(view)
            .setPositiveButton(getString(R.string.text_ok),null)
            .create()
    dialog.setTitle("Acceso Supervisor")
    var btnOk = dialog?.getButton(AlertDialog.BUTTON_POSITIVE)
    dialog.setOnShowListener(object: DialogInterface.OnShowListener{
        override fun onShow(dialog: DialogInterface?) {
            var btnOk1 = dialog.let{ btnOk }
            btnOk1?.setOnClickListener(object : View.OnClickListener{
                override fun onClick(v: View?) {
                    var pass = editPass.getText().toString().trim();
                    if (pass.equals("")){
                        Toast.makeText(getContext(), "Ingresa una contraseña valida", Toast.LENGTH_SHORT).show();
                    } else {
                        if (UsuarioModel.verifySupervisorPassword(getContext(), pass)){
                            dialog?.dismiss()
                            val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                            imm.hideSoftInputFromWindow(editPass.windowToken, 0)
                            var rrf = ReporteRetirosFragment.newInstance()
                            fragmentManager?.beginTransaction()?.replace(R.id.contenedor_reportes_tpv, rrf)
                                    ?.addToBackStack("reporteRetirosTpvFragment")?.commit()
                        } else {
                            Toast.makeText(getContext(), "Oops! contraseña incorrecta", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            })
        }
    })
    dialog.setCanceledOnTouchOutside(false)
    dialog.show()

Does anyone know what is wrong with me in Kotlin, please?

A: ok I just went ahead and tried to remake your xml file. I hope it's accurate enough.
Imagine this is your xml file to be inflated into the alert dialog:
<TextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="Title"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/editPass"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:hint="Password"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtTitle" />

<Button
    android:id="@+id/btnOK"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="OK"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editPass" />

Then this code should work and it is much cleaner than what you had written (no offence):
    val view : View = LayoutInflater.from(this).inflate(R.layout.dialog_password_confirm, null)
    view.txtTitle.text = "Acceso Supervisor"
    view.btnOK.text = getString(R.string.text_ok)
    val myAlertDialog = AlertDialog.Builder(this)
        .setView(view)
        .setCancelable(false)
        .show()
    myAlertDialog.btnOK.setOnClickListener {
        val pass = myAlertDialog.editPass.text.toString().trim()
        if (pass.isBlank()) {
            Toast.makeText(this, "Ingresa una contraseña valida", Toast.LENGTH_SHORT).show()
        } else {
            if (UsuarioModel.verifySupervisorPassword(getContext(), pass)) {
                myAlertDialog.dismiss()
                val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(myAlertDialog.editPass.windowToken, 0)
                var rrf = ReporteRetirosFragment.newInstance()
                fragmentManager?.beginTransaction()?.replace(R.id.contenedor_reportes_tpv, rrf)?.addToBackStack("reporteRetirosTpvFragment")?.commit()
            } else {
                Toast.makeText(this, "Oops! contraseña incorrecta", Toast.LENGTH_SHORT).show();
            }
        }
    }


A: It might be failing because you're calling getButton immediately after creating the Dialog, before you've called show and before it's done a layout pass, so the button might not have been created yet. Your Java code grabs the button during the onShow callback, which happens after it's displayed.
Personally I'd put your validation code in another function, and do something like this:
val dialog: AlertDialog = builder.setView(view)
        .setTitle("Acceso Supervisor")
        .setCancelable(false)
        .setPositiveButton(getString(R.string.text_ok)) { handleClick() }
        .create()

and then
fun handleClick() {
    val pass = myAlertDialog.editPass.text.toString().trim()
        if (pass.isBlank()) {
    ....
}

you'll need to store a reference to your created Dialog so you can dismiss it in that function, since you're not using a DialogFragment here.
(also your Java code is different - you're doing setCancelable(false) but not in your Kotlin code, and you're also checking for an empty or null password, but in Kotlin you're only checking for an empty one - you can do if (pass.isNullOrBlank()) and that should handle all whitespace characters, and you don't need to trim() the string)
