Praanto
Praanto

Reputation: 370

How do I change the width and height of AlertDialog in Kotlin

How do I change the height and width of my Alert dialog in Kotlin. My alert dialog code looks like this:

val loginProgressDialog = AlertDialog.Builder(this)
            .setView(layoutInflater.inflate(R.layout.alert_dialog, null))
            .setCancelable(false)
            .create()

I tried the following but it didn't work

    loginProgressDialog.window?.attributes?.width = 100
    loginProgressDialog.window?.attributes?.height = 100

If you can link to a post where there is a solution, I'd love that too.

Upvotes: 0

Views: 2031

Answers (2)

hemdroid
hemdroid

Reputation: 141

If you are using custom view for your Alert Dialog, you can set the root layout height and width to wrap content and then add another layout with fixed dimensions inside that. This will make your dialog to inherit the same dimensions as the child view.

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:background="@drawable/popup_bg"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">
    
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Ahmed Hegazy
Ahmed Hegazy

Reputation: 12605

You can achieve that by using this

loginProgressDialog.window?.setLayout(100, 100)

You should only use it right after showing the AlertDialog using loginProgressDialog.show()

Upvotes: 2

Related Questions