Solution 1 :
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()
Just a tip: use app:endIconMode="password_toggle"
instead of app:passwordToggleEnabled="true"
(deprecated)
Problem :
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 –
Comments
Comment posted by Rajesh kumar
It’s still showing cropped border even after using Material Dialog.
Comment posted by Gabriele Mariotti
@Rajeshkumar only on API 22 or also in other version (try with the emulator)?