Search code examples
androidandroid-layoutandroid-fragmentsandroid-toolbarmenuitem

Toolbar menu icon not display


I know may be duplicate but i am not finding any solution for that. Actually, i want to show menu with text and icon inside fragment which having a toolbar, i have just add one line in fragment to show menu is

    class JustTry : Fragment(){

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater?.inflate(R.layout.fragment_try, container, false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        var mToolbar = view!!.findViewById<Toolbar>(R.id.toolbar)
        mToolbar.inflateMenu(R.menu.dashboard_menu)
    }

}

enter image description here

I got this output from this code.

here is my menu xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/nav_home"
        android:icon="@mipmap/ic_launcher"
        android:title="Home" />
    <item
        android:id="@+id/nav_messages"
        android:icon="@mipmap/ic_launcher"
        android:title="Messages" />

</menu>

My question is why i am not getting Icon in Message and Home items even i am adding android:icon tag. Any help will be appreciated.


Solution

  • You can try

     <?xml version="1.0" encoding="utf-8"?>
     <menu xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:balloonberry="http://schemas.android.com/apk/res-auto">
    
      <item
         android:id="@+id/menu_item"
         android:icon="@drawable/img_menu"
         balloonberry:showAsAction="always">
         <menu>
             <item
                 android:id="@+id/btn_delete"
                 android:title="delete"
                 android:icon="@android:drawable/ic_delete"/>
             <item
                 android:id="@+id/btn_message"
                 android:title="Message"
                 android:icon="@android:drawable/ic_dialog_alert"/>
    
    
              </menu>
          </item>
    
    
     </menu>