3

Here is the custom layout password_alert.xml

I have tried increasing the padding in ConstraintLayout. But it still did not work.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til_password"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:hint="@string/enter_password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:passwordToggleEnabled="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_enter_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:singleLine="true" />
    </com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Below is the code for alert dialog

val enterPassDialog = AlertDialog.Builder(this)
        enterPassDialog.setTitle("Password Protected")
        val pass = layoutInflater.inflate(R.layout.enter_password_dialog,null)
        val etPassword = pass.findViewById<EditText>(R.id.et_enter_password)
        enterPassDialog.setView(pass)

        enterPassDialog.setNegativeButton("CANCEL") { dialog, _ ->
            dialog.cancel()
            finish()
        }

        enterPassDialog.setPositiveButton("OK") { _, _ ->

        }

        enterPassDialog.setOnCancelListener { dialog ->

        }

        val enterDialog = enterPassDialog.create()
        enterDialog.show()

I am using Android API 22 as a test device. Here is the output image -

alert dialog image

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Rajesh kumar
  • 982
  • 1
  • 9
  • 17

1 Answers1

0

Just use the MaterialAlertDialogBuilder with the same layout:

    MaterialAlertDialogBuilder(context)
            .setTitle("Dialog") 
            .setView(R.layout.password_alert) 
            .setPositiveButton("Ok",  /* listener = */null)
            .setNegativeButton("Cancel",  /* listener = */null)
            .show()

enter image description here

Just a tip: use app:endIconMode="password_toggle" instead of app:passwordToggleEnabled="true"(deprecated)

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841