I followed the steps according to the their official website. Still it doesn't show up items on the actionbar. I also updated the theme name on manifest file.
Any help?
Your question is so general but I think you need to change your menu xmls. They are under res/menu. If the name of your class is main, then menu should be in menu_main.xml. You should change app:showAsAction to ifRoom or always.
<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=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="always" android:icon="#drawable/ic_action_important" />
</menu>
Related
I tried to add an icon for "Create Order" to be displayed on the action bar, but the icon is not being displayed instead on the action bar only 3 dots are being showed and inside them there is written "Create Order".
The following is my xml code:
<?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=".MainActivity">
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:icon="#drawable/ic_add_black_24dp"
android:orderInCategory="1"
app:showAsAction="always">
</item>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
Use app:showAsAction to ifRoom as below:
<?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/create_order"
android:icon="#drawable/ic_add_black_24dp"
android:title="#string/action_create_order"
app:showAsAction="ifRoom" />
</menu>
Note:
The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar. If you set app:showAsAction="ifRoom" (as in the example code's favorite action), the action is displayed as a button if there is room in the app bar for it; if there is not enough room, excess actions are sent to the overflow menu. If you set app:showAsAction="never" (as in the example code's settings action), the action is always listed in the overflow menu, not displayed in the app bar.
Refer below link for more details:
Add Action Buttons
If you want to show the Icon and the Text "Create Order" you can use:
app:showAsAction="ifRoom|withText"
of course, it can show only, if your view has enough space for these elements.
I am using v7 Support library and using app namespace in the menu_main.xml file. Even then, the action is never displayed in the action bar but in the overflow bar. This happens even when I use app:showAsAction="always"
menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:orderInCategory="1"
app:showAsAction="always"
android:icon="#drawable/add_box_black_icon"
/>
</menu>
The order of the items is important - swap the create order and settings if you want to achieve your original idea.
After 2 days of struggling with the new API 21 Toolbar and appCompat_v7, I think I found a bug on it. If you have 2 actions on your menu like this:
<item
android:id="#+id/action_test"
android:showAsAction="always"
android:icon="#drawable/ic_launcher"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
and a appCompat toolbar defined like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.toolbar.MainActivity" >
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="52dp"
android:id="#+id/toolbar">
</android.support.v7.widget.Toolbar>
</RelativeLayout>
after inflating (or setting the setSupportActionBar method)
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("esurance");
setSupportActionBar(toolbar);
you will get the toolbar menu without your action icon, it will display it on the overflow menu.
But, if you use the Toolbar class from API 21, it will show your actions as defined on your menu layout...
<Toolbar
android:layout_width="match_parent"
android:layout_height="52dp"
android:id="#+id/toolbar">
</Toolbar>
Maybe I'm missing something here, but so far, I've been unable to display actions outside the overflow menu using appCompat. Any help on this will be much appreciated.
Per the Action Bar training, you have to use the app:showAsAction attributes rather than the android:showAsAction attribute:
Notice that the showAsAction attribute above uses a custom namespace defined in the <menu> 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 file should look like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_test"
app:showAsAction="always"
android:icon="#drawable/ic_launcher"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
put
xmlns:app="http://schemas.android.com/apk/res-auto" int your menu tag
like this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
then use app:showAsAction = "always"
Remember that use app:showAsAction in all menu item not android:showAsAction
`
first of all use the app:showAsAction instead of android:showAsAction (like #ianhanniballake)
after that in onCreateActionMode after Inflating your menu setShowAsAction
in code like this
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.your_menu_name, menu);
menu.findItem(R.id.your_first_menu_id).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.findItem(R.id.your_second_menu_id).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
i had the same problem so after working on it these lines of code works fine for me.
Hi I am developing sample Android application in which I am trying to display menu items in actionbar. But my actionbar always showing overflow menu even if there is only single menu item. It is not showing my menu items image in action bar. I have implemented this in following manner
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.samplechromiapp.MainActivity" >
<item
android:id="#+id/categories"
android:icon="#drawable/navigation_collapse"
android:showAsAction="ifRoom|withText"
android:title="Categories"
android:visible="true"
/>
</menu>
Am I doing anything wrong? Need some help. Thank you.
Try this code and add extra xmlns attribute for your menu and try app:showAsAction instead of android:showAsAction
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/categories"
android:icon="#drawable/navigation_collapse"
android:title="Categories"
app:showAsAction="always"/>
</menu>
I have an action bar with the follwing menu:
<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="br.com.putzbati2.PrincipalActivity" >
<item android:id="#+id/action_mensagem"
android:title="Mensagem"
android:icon="#drawable/barra_superior_envelope"
android:showAsAction="always|withText" />
<item android:id="#+id/action_carro"
android:icon="#drawable/barra_superior_carro"
app:showAsAction="always"
android:title="Meus Carros"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
But the withText doesnt work. It shows only the icon.
I've already try to use android:showAsAction instead of app:showAsAction and when I do this, it doesnt show the icon neither the text. Does any one know how to help me?
I've found this solution, but as I said, it doesnt work for me
app:showAsAction ifRoom is not working on appcompat action bar
You can use like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_mensagem"
android:title="#string/id_item_mensagem"
android:icon="#drawable/id_item_mensagem"
android:showAsAction="always|withText" />
<!-- ... another items -->
</menu>
'always|withText' will work if there is enough room, otherwise it will only place icon! You are using so much items with text, probably you don't have room. So you will see only the icon.
You can check it if you rotate your application to landscape.