Search code examples
kotlinandroid-alertdialoglayout-inflaterrounded-corners

Rounded corners for custom AlertDialog


I have been updating my apps forgot password functionality from Activity to simple custom Alert Dialog. How should I add rounded corners to Dialog window? I have already read multiple tutorials, but none seem to work in my case. I read from somewhere that I should use setBackgroundResources method somewhere, but I'm not sure where.

Kotlin code

// Forgot password textview onClick Listener
    binding.tvForgotPassword.setOnClickListener {

        // Inflate add_item_dialog.xml custom view
        val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_forgot_password, null)

        // Add AlertDialog Builder
        val dialogBuilder = AlertDialog.Builder(this)
            .setView(dialogView)

        // Binding add_item_dialog layout
        val dialogBinding = DialogForgotPasswordBinding.bind(dialogView)

        // Show Forgot password Dialog
        val customAlertDialog = dialogBuilder.show()
        dialogBinding.ivCloseDialog.setOnClickListener {
            customAlertDialog.dismiss()
        }
    }

dialog_forgot_password.xml

<androidx.cardview.widget.CardView
    android:layout_width="350dp"
    android:layout_height="500dp"
    app:cardCornerRadius="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="350dp"
        android:layout_height="500dp">


        <ImageView
            android:id="@+id/iv_closeDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.94"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.060000002"
            app:srcCompat="@drawable/ic_close" />

        <TextView
            android:id="@+id/tv_forgotPasswordInfo"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="center"
            android:textSize="18sp"
            android:text="@string/enter_email"
            android:textColor="@color/ColorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_forgotPassword" />

        <Button
            android:id="@+id/submitBtn"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/button"
            android:elevation="4dp"
            android:text="@string/submit"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/emailForgot"
            app:layout_constraintVertical_bias="1.0" />

        <TextView
            android:id="@+id/tv_forgotPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="36dp"
            android:fontFamily="sans-serif-black"
            android:text="@string/forgot_password"
            android:textColor="@color/ColorPrimary"
            android:textSize="40sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/iv_closeDialog" />

        <EditText
            android:id="@+id/emailForgot"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:background="@drawable/rounded_edittext"
            android:ems="10"
            android:hint="@string/email_address"
            android:importantForAutofill="no"
            android:inputType="textEmailAddress"
            android:paddingStart="15dp"
            android:textColorHint="@color/ColorPrimary"
            app:layout_constraintBottom_toTopOf="@+id/submitBtn"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_forgotPasswordInfo"
            app:layout_constraintVertical_bias="1.0" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

Rounded_framelayout.xml

<solid android:color="#FFFFFF"/>


<padding android:left="2dp"
    android:top="2dp"
    android:right="2dp"
    android:bottom="2dp"/>

<corners android:radius="20dp" />

Solution

  • Please take a look at this guide for how to customize dialogs.

    In short, you need to create a dialog like normal, but set the background resource before you show it:

    val builder = AlertDialog.Builder(context)
    builder.setView(R.layout.item_dialog)
    val dialog = builder.create()
    dialog.window?.decorView?.setBackgroundResource(R.drawable.dialog_background) // setting the background
    dialog.show()
    

    where dialog_background is defined like so:

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="16dp" />
        <solid android:color="?android:colorBackground" />
    </shape>