Search code examples
androidandroid-actionbartoolbarandroid-menu

menu items not showing on toolbar


I have searched and found other as well with the same problem but still I could not resolve the issue of menu items not showing in the action bar. I will post my action bar code, menu_main.xml and MainActivity.

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/ColorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:elevation="10dp"
        >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PROJECT TIMELINE"
        android:id="@+id/toolbar_title"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="@color/ColorPrimaryDark" />

</android.support.v7.widget.Toolbar>

this is from menu_main.xml

<menu 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"
    tools:context=".MainActivity">


    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item android:id="@+id/search"
        android:title="@string/Search"
        android:icon="@drawable/ic_action"
        app:showAsAction="ifRoom"
        />

</menu>

and I wrote this in MainActivity.java

public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

Solution

  • Initialise the Toolbar and call this method in onCreate of your Activity

    setSupportActionBar(yourToolbar);