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.
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.
Noob question. I'm using Toolbar and menu in Android app. How i can to change margin or padding between two menu items?
All answers i found is about Sharlock bar (https://stackoverflow.com/questions/15678467/how-to-add-padding-between-menu-items-in-android) or old Action bar (https://stackoverflow.com/questions/21131814/how-to-padding-between-actionbars-menu-items-icon-when-using-the-support-librare).
I think the answer near with style files, but i realy stack what parent or item name i need for my case.
My sample 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=".MainActivity">
<item android:id="#+id/menu_contents"
android:title="#string/menu_contents"
android:icon="#drawable/ic_format_list_numbered"
android:orderInCategory="2"
app:showAsAction="always" />
<item android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_search"
android:orderInCategory="100"
app:showAsAction="always" />
<item android:id="#+id/menu_settings"
android:title="#string/menu_settings"
android:orderInCategory="100"
app:showAsAction="never" />
Thank you!
You'll want to programmatically assign an ActionProvider using setActionProvider() on the MenuItem that gets created for the items you want to change.
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>
can someone explain why the 2nd item is not being added to the overflow menu in the actionbar? The settings displays correctly, and profile displays if I set the showAsAction to always, but I would rather have profile appear in the overflow menu.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:Molo="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/settings"
Molo:showAsAction="always" />
<item android:id="#+id/action_profile"
android:title="#string/action_profile"
android:icon="#drawable/user"
Molo:showAsAction="never"/>
</menu>
Add android:showAsAction mapped to the same value as Molo:showAsAction, as that should help on newer devices.
just removed the Molo:showAsAction="never" ,Try this..
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:icon="#drawable/settings"
Molo:showAsAction="always" />
<item android:id="#+id/action_profile"
android:title="#string/action_profile"
android:icon="#drawable/user" />