Den  Stre Den Stre - 29 days ago 4
Android Question

Toolbar Won't Show Menu Button

I tried different methods of making a menu button on the Android Studio update but I couldn't make the menu button appear.

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.settings) {
return true;
}

return super.onOptionsItemSelected(item);
}


I have added this code in the Activity and made a menu.xml file containing:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@+id/expanded_menu"
android:showAsAction="always"
android:icon="@drawable/ic_action_menu"
app:showAsAction="always"
android:title="button">
</item>
<item android:id="@+id/credits"
android:title="Credits"
android:orderInCategory="100"
/>
<item android:title="Settings"
android:id="@+id/settings"
android:orderInCategory="101"
/>
</menu>


Anyway, how I can make the three squares that were automatically added in the older versions of Android appear?

App screenshot

This is a screenshot of the application, in previous versions of android studio there were three dots at the top right corner.
Currently they are not there and my question is how do I add the menu button.

Edit: also while writing
app:showAsAction="always"
android studio marked all of the line red untill I wrote the "=" sign, maybe this error is related to something?
Also maybe I missed a step while making the items/toolbar, but I do not know exactly if I missed anything.

Answer

I believe you are talking about the overflow button (labeled '3'):

enter image description here

This button appears when there is not enough room to display all of the items in your options menu, or when you designate specific items to not be shown as an action. This is handled by the app:showAsAction attribute. There are five different values you can apply, which are described as follows:

  • ifRoom : Only place this item in the app bar if there is room for it. If there is not room for all the items marked "ifRoom", the items with the lowest orderInCategory values are displayed as actions, and the remaining items are displayed in the overflow menu.

  • withText : Also include the title text (defined by android:title) with the action item. You can include this value along with one of the others as a flag set, by separating them with a pipe |.

  • never : Never place this item in the app bar. Instead, list the item in the app bar's overflow menu.

  • always : Always place this item in the app bar. Avoid using this unless it's critical that the item always appear in the action bar. Setting multiple items to always appear as action items can result in them overlapping with other UI in the app bar.

  • collapseActionView : The action view associated with this action item (as declared by android:actionLayout or android:actionViewClass) is collapsible. Introduced in API Level 14.

Therefore, if you want to only show the overflow button, you should set app:showAsAction="never" for all of your menu items.

Though, the typical structure for an options menu is to always show the first item, and designate the rest to be shown "ifRoom".