1
votes

I am trying to create a logout menu item in the top actionbar. I performed the following steps:

1) res > right click > new > android resource directory > resource type > menu > ok

2) right click on the newly created menu folder and selected new > file > main_menu.xml

3) Based from these docs https://developer.android.com/guide/topics/resources/menu-resource.html, I added the following xml in the main_menu.xml text view:

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

4) In the main_menu.xml design view, I dragged a menu item into the menu. I gave it an id, a title and showAsAction set to always. I then returned to text view and noticed the following generated:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="Logout" android:id="@+id/@+id/logout" android:showAsAction="always"/>
</menu>

5) I fixed the id and noticed showAsAction it was giving the following message:

Should use app:showAsAction with appcompat library with xmlns:app="schemas.android.com/apk/res-auto"'; 

I ran the app and the menu item never showed up.

Didding through research, I changed the xml to this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_settings" android:title="Logout"
          android:orderInCategory="100" app:showAsAction="always"/>
</menu>

I even tried add this to the xml:

xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"

But the menu item is not showing up on the MainActivity screen.

2

2 Answers

0
votes

You should inflate the menu in onCreateOptionMenu method of activity: getMenuInflater().inflate(R.menu.main_menu,menu)

0
votes

These are also useful notes from that article:

The toolbar has been introduced in Android 5.0 (API 21). If you want to use the toolbar on devices with an earlier Android release you can use the downport provided by the appcompat-v7 support library.

Applications with a target SDK version less than API 11 use the options menu, if such a button is present on the device. The option menu is displayed if the user presses the Option button. The toolbar bar is superior to the options menu, as the action bar is clearly visible, while the options menu is only shown on request.

Entries in the toolbar are typically called actions. While it is possible to create entries in the action bar via code, it is typically defined in an XML resource file. Each menu definition is contained in a separate file in the res/menu folder. The Android tooling automatically creates a reference to menu item entries in the R file, so that the menu resource can be accessed.

The MenuInflator class allows to inflate actions defined in an XML file and adds them to the action bar. MenuInflator can get accessed via the getMenuInflator() method from your activity.

@Override public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu); return true; }

If an action is selected, the onOptionsItemSelected() method in the corresponding activity is called. It receives the selected action as parameter.

@Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { You can change the visibility of the toolbar at runtime. The following code demonstrates that.

ActionBar actionBar = getActionBar(); actionBar.hide(); actionBar.show();