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); }
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.
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.
Action Bar/Toolbar/App Bar It is a menu bar that runs across the top of the activity screen in android.
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.
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();
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With