Android DialogFragment Fullscreen Like Activity

We can use DialogFragment to show a fullscreen overlay (like loading an Activity, but with Fragment).

Why not use a Fragment instead of DialogFragment. With Fragment, you would need to prepare a fullscreen layout and load/replace the Fragment into the layout. With DialogFragment, there is no need of such layout.

The following code and layout will create a layout with black overlay which filled 90% of the screen.

class GiftDialog : DialogFragment() {    companion object {        private const val FRAGMENT_TAG = "gift_dialog"        fun newInstance() = GiftDialog()        fun show(fragmentManager: FragmentManager): GiftDialog {            val dialog = newInstance()            // dialog.isCancelable = false            dialog.show(fragmentManager, 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.gift, container)    }}
<?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="match_parent"
        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 you use LinearLayout as root layout, the dialog size will shrink to minimum. Refer Android DialogFragment Match Parent Width and Height.

To match 100% with and height (like an Activity) without black overlay background.

class GiftDialog : DialogFragment() {    ...    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_NoActionBar)        // this will make it fullscreen without top status bar        // setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen)    }}

❤️ 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.