Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionbar shows dropdown when only one item in the menu

Im using app compat theme in my app, and in the actionbar I have a menu with one item that should be shown as an icon - but instead I get menu button like this

enter image description here

Here is my code of the menu xml:

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

    <item
        android:id="@+id/action_play"
        android:icon="@drawable/play"
        android:orderInCategory="100"
        android:showAsAction="always"/>

</menu>

And here is the code on how I initialize the actionbar:

getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setTitle(title);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(false);

and show the menu:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.names, menu);
        playStop = menu.findItem(R.id.action_play);
        return true;
    }

So can someone help me to display the menu item as it should?

Thanks

like image 424
Darko Petkovski Avatar asked Apr 30 '15 05:04

Darko Petkovski


People also ask

How do I hide a menu item in the ActionBar?

You can set a flag/condition. Call invalidateOptionsMenu() when you want to hide the option. This will call onCreateOptionsMenu() .

Which method is used to create menu items on ActionBar?

The Android tooling automatically creates a reference to menu item entries in the R file, so that the menu resource can be accessed. An activity adds entries to the action bar in its onCreateOptionsMenu() method. The showAsAction attribute allows you to define how the action is displayed.

How do I add settings to action bar?

First, create a menu resource, name it whatever you want. The item tag defines the id, the icon and the title. the showAsAction defines if item is located at the action bar or submenu. It's recommended to use string resources for your title, specially if you want to translate your app.

How do I replace my action bar toolbar?

You'll want to add android:fitsSystemWindows="true" to the parent layout of the Toolbar to ensure that the height of the activity is calculated correctly. When using the support library, make sure that you are importing android. support. v7.


1 Answers

Using support library, android:showAsAction will not do its task. So your menu declaration is not correct. From documentation

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
</menu>

If there's not enough room for the item in the action bar, it will appear in the action overflow.

Using XML attributes from the support library

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.


So your menu xml should be

<?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">

    <item
        android:id="@+id/action_play"
        android:icon="@drawable/play"
        android:orderInCategory="100"
        app:showAsAction="always"/>

</menu>
like image 109
Pankaj Kumar Avatar answered Oct 07 '22 07:10

Pankaj Kumar