I cannot get icons on the action bar - android

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.

Related

Hide the top level menu when pop up sub menu is displayed in Android app

Android app has one top level menu named "Organisation" which gets displayed on the click of ellipsis (3 dots).
On touching this menu, pop up submenu gets displayed with top item as Organisation.
I want to hide this top level menu
and
only show 2 items in the sub pop up menu.
"Register Org" and "Change Org" are apps 2 sub popup menu items.
How do I do that? I tried below code which doesn't work.
MenuItem topLevelMenuOrganisation = menuList.findItem(R.id.idMenuOrganisation);
topLevelMenuOrganisation.setVisible(false);
Menu.xml
<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="com.example.jayantbhave.menuapps.MainActivity">
<item
android:id="#+id/idMenuOrg"
android:title="Organisation">
<menu>
<item
android:id="#+id/idMenuRegisterOrg"
android:title="Register Org" />
<item
android:id="#+id/idMenuChangeOrg"
android:title="Change Org" />
</menu>
</item>
</menu>
You can remove the "Organisation" item from the menu. Write your menu as following:
<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="com.example.jayantbhave.menuapps.MainActivity">
<item
android:id="#+id/idMenuRegisterOrg"
android:title="Register Org" />
<item
android:id="#+id/idMenuChangeOrg"
android:title="Change Org" />
</menu>
Hope it helps!

Action item always appears in the overflow menu

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.

Custom Layout menu item shows empty if showAsAction=never

I am trying to add SwitchCompat to Overflow menu using this code below:
main.xml
<?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/menu_dummy_content"
android:title=""
android:actionViewClass="android.support.v7.widget.SwitchCompat"
app:showAsAction="never"
app:actionLayout="#layout/menu_item_switch"/>
<!-- TODO: Delete this after bug fix-->
<item
android:id="#+id/menu_dummy_content_2"
android:title="Second Title"
app:showAsAction="never"/>
</menu>
The layout for menu_item_switch is:
<android.support.v7.widget.SwitchCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_item_switch_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/menu_item_dummy_tasks"
android:layout_centerVertical="true"/>
If I do app:showAsAction="ifRoom" or anything, the switch appears perfectly fine and the listeners work fine. But as soon as I make the showAsAction as never. The switch view becomes white/empty but the onOptionsItemSelected says the clicked item is menu_dummy_content.
I also tried using only TextView in the custom layout, and that too comes as empty. Am I missing something?
Since you set the showAsAction attribute to never, then these menu item will never show as action layout.
I believe you need to add Title Text in here...
<item
android:id="#+id/menu_dummy_content"
android:title=""
android:actionViewClass="android.support.v7.widget.SwitchCompat"
app:showAsAction="never"
app:actionLayout="#layout/menu_item_switch"/>
like this...
android:title="First Title"

app:showAsAction="always|withText" not showing text

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.

Create ActionBar and options menu in same activity

I'm trying to show same menu options both in the ActionBar and in the menu options but it shows up only in one of them, is there a simple way to do it?
<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="com.example.app.MainActivity" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="always"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
Now the items appear in the action bar.
Changing the app:showAsAction to android:app:showAsAction shows the menu items at the options menu only, any way to make them appear in both menus?
Thanks
Change showAsAction="always" to showAsAction="never" for which you want to display in option menu at bottom...
See showAsAction element in below link for more details...
http://developer.android.com/guide/topics/resources/menu-resource.html

Categories

Resources