Android toolbar menu is not showing

74,671

Solution 1

I'm not sure why, but when i place everything related menu inflating in onPrepareOptionsMenu method, everything works fine.

@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.dashboard, menu);

    return super.onCreateOptionsMenu(menu);
}

Solution 2

I was also facing the same problem, but the actual error was, i forgot to introduce toolbar in java activity

under AppCompactActivity, under on create method define your toolbar by id and call setSupportActionBar(ToolBar);

Example is below:

public class secondActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.showOverflowMenu();

Solution 3

If you are inflating your menu from a fragment, e.g. by overriding onCreateOptionsMenu method, make sure to call setHasOptionsMenu(true) in onCreateView of your fragment

Solution 4

Try the following:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      tools:context="com.example.lolipoptest.MainActivity" >

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

and the Java Code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

Solution 5

Do you have Toolbar in your dashboard layout ?. Call setSupportActionBar(toolbar) in your activity. Use Theme.AppCompat.NoActionBar theme for the activities (If you are using toolbar in it)

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;

public class DashboardActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  getSupportActionBar().setHomeButtonEnabled(true);
}

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.dashboard, menu);
  return super.onCreateOptionsMenu(menu);
 }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

Check your styles.

<resources>

<!-- Base application theme. -->
<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme" parent="AppTheme.Base">

</style>

<style name="ToolbarTheme" parent="AppTheme" >
</style>

<color name="light">#FFBB33</color>
<color name="colorPrimary">#FFBB33</color>
<color name="textColorPrimary">#FFBB33</color>
<color name="colorPrimaryDark">#FF8800</color>
<color name="colorAccent">#ff9977</color>
<color name="white">#ffffff</color>

</resources>

Check your layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    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="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:titleMarginStart="20dp"
    android:paddingRight="10dp"
    android:background="@color/colorPrimaryDark"
    app:theme="@style/ToolbarTheme" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:text="Hello Toolbar" />

</LinearLayout>

Add theme in your activity in manifest

android:theme="@style/AppTheme"
Share:
74,671
Procurares
Author by

Procurares

Updated on January 19, 2022

Comments

  • Procurares
    Procurares over 2 years

    I'm trying to add a menu to the ToolBar. onCreateOptionsMenu method of my Activity is called, but no menu appears.

    This is dashboard.xml (from menu folder)

    <?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"
          xmlns:tools="http://schemas.android.com/tools"
          tools:context="com.app.android.ui.dashboard.DashboardActivity">
    
        <item
            android:id="@+id/action_scan_qr"
            android:icon="@drawable/ic_drawer"
            android:title="@string/menu_scan_qr"
            app:showAsAction="always" />
    </menu>
    

    NOTE: icon of this menu is darker than the background color of the action bar, so it should be visible.

    Inflating menu in Activity:

    public class DashboardActivity extends ActionBarActivity {
    
    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.dashboard, menu);
    
        return true;
    }
    

    And the main theme for the application:

    <style name="Theme.Application.Base" parent="Theme.AppCompat.Light">
            <item name="colorPrimary">@android:color/white</item>
            <item name="colorPrimaryDark">@android:color/white</item>
            <item name="android:windowNoTitle">true</item>
            <item name="windowActionBar">false</item>
            <item name="drawerArrowStyle">@style/Theme.Application.DrawerArrowStyle</item>
            <item name="android:textColorSecondary">@android:color/darker_gray</item>
    </style>
    

    Why onCreateOptionsMenu is called but menu doesn't appears. I'm using appcompat-v7:21.0.3

    EDIT:

        @Override
        protected void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(getContentViewId());
    
            toolbar = (Toolbar) findViewById(R.id.tool_bar);
            if (toolbar == null) {
                throw new Error("Can't find tool bar, did you forget to add it in Activity layout file?");
            }
    
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }
    
  • pablisco
    pablisco over 9 years
    Not entirely true, the library adds the toolbar if not present and the right theme is used
  • Akash
    Akash over 9 years
  • cimenmus
    cimenmus about 8 years
    it is correct. i had the same problem. i did you said and it solved
  • Jan Šimek
    Jan Šimek about 8 years
    It does work indeed, but why? What is the purpose of inflate()? The official documentation doesn't even mention this.
  • Vinay Bhargav
    Vinay Bhargav about 8 years
    It could be just overlapping color problem. If your toolbar is black color and the menu text is also in black color, you can't really see it. Even overflow menu icon was also black :D (It happened in my case). Further dismay- if there are only one or two menu options, even menu button does not seem to work (because menu is already present in toolbar)
  • Adnan Ali
    Adnan Ali about 8 years
    it will continuously create when ever it is clicked,So use this @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; }
  • Dinesh
    Dinesh almost 8 years
    thanks Bro it was surprisingly acting. fabulous +1 for you
  • kingston
    kingston over 7 years
    Do you really call super.onCreateOptionsMenu(menu) and not super.onPrepareOptionsMenu(menu)?
  • Antroid
    Antroid about 7 years
    i had everything set up but didnt use setSupportActionBar(ToolBar); introducing setSupportActionBar made the action button show up
  • kostyabakay
    kostyabakay almost 7 years
    It will loop the options menu
  • Nicks
    Nicks over 6 years
    Thanks you save my hours :)
  • Inder
    Inder almost 6 years
    kindly consider adding more information in your answer
  • Subhasmith Thapa
    Subhasmith Thapa about 5 years
    Thanks. Saved my time
  • RandomEngy
    RandomEngy over 4 years
    Should be returning "true" at the end: developer.android.com/guide/topics/ui/menus
  • RandomEngy
    RandomEngy over 4 years
    Should be returning "true" at the end: developer.android.com/guide/topics/ui/menus . Also the recommendation is to override onCreateOptionsMenu instead of onPrepareOptionsMenu.
  • mufazmi
    mufazmi about 4 years
    nope, when you do this, and error will be show you.