简体   繁体   中英

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.

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()

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM