Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android toolbar menu is not showing

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);     } 
like image 879
Procurares Avatar asked Feb 04 '15 09:02

Procurares


People also ask

How do I show the menu bar on Android?

This is going to be in res/menu/main_menu . Right click the res folder and choose New > Android Resource File. Type main_menu for the File name. Choose Menu for the Resource type.

How do I get my Android toolbar icon back?

Show back button using actionBar. setDisplayHomeAsUpEnabled(true) this will enable the back button. Custom the back event at onOptionsItemSelected. This will enable the back function to the button on the press.

Where is toolbar located in Mobile?

Action Bar/Toolbar/App Bar It is a menu bar that runs across the top of the activity screen in android.

What is Toolbar in Android app?

In Android applications, Toolbar is a kind of ViewGroup that can be placed in the XML layouts of an activity. It was introduced by the Google Android team during the release of Android Lollipop(API 21). The Toolbar is basically the advanced successor of the ActionBar.


2 Answers

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(); 
like image 115
Shubham Avatar answered Oct 04 '22 20:10

Shubham


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); } 
like image 26
Procurares Avatar answered Oct 04 '22 20:10

Procurares