Android Add Toolbar to DialogFragment

Toolbar inflate menu, handle back button and menu click without Activity
// inflate menutoolbar.inflateMenu(R.menu.menu_photo_pin)// include a back icontoolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp)toolbar.setNavigationOnClickListener {    dismiss()}// handle menu item clicktoolbar.setOnMenuItemClickListener {    when (it.itemId) {        R.id.action_share_photo -> {            // do somethong            true        }        else -> {            false        }    }}

Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

NOTE: Strangely, the DialogFragment width and height shrink to minimum when using LinearLayout, but looks fine when using RelativeLayout.

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