Android DialogFragment Match Parent Width and Height (90% size with black opacity background)

DialogFragment size too small

Solution 1: User relative layout

If you use LinearLayout for the DialogFragment layout, the dialog size will shrink to minimum.

The problem can be solved by using RelativeLayout, where dialog is shown with normal size.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="?dialogPreferredPadding"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello World" />

            />
    </LinearLayout>
</RelativeLayout>

NOTE: If LinearLayout.layout_height="wrap_content", the dialog will match content size. If LinearLayout.layout_height="match_parent", the dialog will go full/90% height.

class TestDialog : DialogFragment() {    companion object {        private const val FRAGMENT_TAG = "test_dialog"        fun newInstance() = TestDialog()        fun show(supportFragmentManager: FragmentManager): SyncSheetsDialog {            val dialog = newInstance()            dialog.show(supportFragmentManager, FRAGMENT_TAG)            return dialog        }    }    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {        // return super.onCreateView(inflater, container, savedInstanceState)        return activity!!.layoutInflater.inflate(R.layout.test, container)    }    override fun onStart() {        super.onStart()    }}

Solution 2: dialog.window.setLayout

class TestDialog : DialogFragment() {    override fun onStart() {        super.onStart()        dialog?.window?.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)    }}

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.