I am trying to add a shopping cart icon with the code below the output I get is two options namely create order and order in settings, and Icon is nowhere to be seen.
Can anyone tell me what's wrong in my code?
My code:
<?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:orderInCategory="100"
app:showAsAction="never"
android:title="order" />
<item
android:title="#string/create_order"
android:id="#+id/action_create_order"
android:orderInCategory="1"
android:icon="#drawable/ic_add_shopping_cart_black_48dp"
app:showAsAction="always"
/>
</menu>
Your xmlns android schema is wrong. you have written this :
xmlns:android= "https://schemas.android.com/apk/res/android"
instead of this write this :
xmlns:android="http://schemas.android.com/apk/res/android"
Try this code :
<?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:orderInCategory="100"
app:showAsAction="never"
android:title="order" />
<item
android:title="#string/create_order"
android:id="#+id/action_create_order"
android:orderInCategory="1"
android:icon="#drawable/ic_album"
app:showAsAction="always"
/>
</menu>
Since you set the showAsAction attribute to never, then these menu items will never show as action views. Try this:
<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android= "https://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"
android:showAsAction="ifRoom|withText"
/>
<item
android:title="#string/create_order"
android:id="#+id/action_create_order"
android:orderInCategory="100"
android:icon="#drawable/ic_add_shopping_cart_black_24dp"
android:showAsAction="ifRoom|withText"
app:showAsAction="ifRoom"
/>
</menu>
Related
I am trying to show some icons in the toolbar using the following menu but in the output Instead of icons, only text is displaying. How to display icons instead of text
menu.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">
<item
android:id="#+id/toEditName"
android:title="#string/buttonEditName"
app:showAsAction="ifRoom"
tools:icon="#drawable/ic_edit_name" />
<item
android:id="#+id/toHistory"
android:title="#string/buttonHistory"
app:showAsAction="ifRoom"
tools:icon="#drawable/ic_history" />
</menu>
Preview Image
Actual Output Image
You have to use android:icon attribute not tools:icon Try this
<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">
<item
android:id="#+id/toEditName"
android:title="#string/buttonEditName"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_edit_name" />
<item
android:id="#+id/toHistory"
android:title="#string/buttonHistory"
app:showAsAction="ifRoom"
android:icon="#drawable/ic_history" />
</menu>
Your icons are not displayed because you are using tools attribute, which show the icon when you are working in the android studio. If you run the app the icons are not displayed.
<?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/toEditName"
android:icon="#drawable/ic_edit_name"
android:title="#string/buttonEditName"
app:showAsAction="ifRoom" />
<item
android:id="#+id/toHistory"
android:icon="#drawable/ic_history"
android:title="#string/buttonHistory"
app:showAsAction="ifRoom" />
</menu>
I hope it helps.
it was a bit late, but tools attribute usually is used to display something in your Android Studio xml preview. That is why your icon not showing.
<?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/multi_select_reply"
android:title="#string/reply"
android:icon="#drawable/ic_reply_svg" <--- change tools:icon to android:icon
android:iconTint="#color/colorWhite"
app:showAsAction="ifRoom"/>
</menu>
Hope it helps to you!
I need to trigger a function on clicking the menu icon, and I don't need to show any items inside the menu. When I tries to avoid all the items inside the menu tag, the whole icon itself gets invisible. So how could I display only the menu icon and hide its sub items?
<?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_settings"
android:orderInCategory="100"
android:icon="#drawable/ic_menu_camera"
android:title="Settings"
app:showAsAction="never" />
//removing the above item removes the whole menu title icon.
</menu>
you need to change app:showAsAction="always", 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/action_settings"
android:icon="#drawable/ic_menu_camera"
android:orderInCategory="100"
android:title="Settings"
app:showAsAction="always" />
</menu >
check this https://developer.android.com/guide/topics/resources/menu-resource.html
Either use visible = false
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:icon="#drawable/ic_menu_camera"
android:title="Settings"
android:visible=false
app:showAsAction="never" />
Or Use a Toolbar inside which add a Menu icon and set onCLickListener().
I'm trying use Navigation Drawer. Everything is okay, but there is an error here:
<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_example"
android:orderInCategory="100"
android:title="#string/action_example"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
android:showAsAction="ifRoom"
/>
</menu>
Red underlined (two)line is:
android:showAsAction="ifRoom"
Information message is here
And This line is grayed.
xmlns:app="http://schemas.android.com/apk/res-auto"
Any help can be help!
change this line
android:showAsAction="ifRoom"
to
app:showAsAction="ifRoom"
And Clean Build your Project.You are done.Hope it will help.Thanks
so i made my own toolbar with a menu and i am infalting it:
toolbarBottom.inflateMenu (R.menu.user_interaction);
and this is the menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res/android">
<item app:title="Edit"
app:id="#+id/post"
app:icon="#drawable/ic_action_pinboard_white"
app:showAsAction="always"
/>
<item app:id="#+id/menu_share"
app:icon="#drawable/ic_action_recent_white"
app:showAsAction="always"
app:title="Undo" />
<item app:id="#+id/test"
app:icon="#drawable/ic_action_groups_white"
app:showAsAction="always"
app:title="Redo" />
</menu>
what i get now is the 3 android option dots on the right side of my bar. If i press it my 3 menuĀ“s appear. What i want is the 3 menu items to appear on the bar itself witht he 3 icon drawables.
Where is my mistake? Did i forgot something? :O
ok i solved it like this:
<?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:title="Edit"
android:id="#+id/post"
android:icon="#drawable/ic_action_pinboard_white"
app:showAsAction="always"
/>
<item android:id="#+id/menu_share"
android:icon="#drawable/ic_action_recent_white"
app:showAsAction="always"
android:title="Undo" />
<item android:id="#+id/test"
android:icon="#drawable/ic_action_groups_white"
app:showAsAction="always"
android:title="Redo" />
</menu>
It 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:title="Edit"
android:id="#+id/post"
android:icon="#drawable/ic_action_pinboard_white"
app:showAsAction="always"
/>
<item android:id="#+id/menu_share"
android:icon="#drawable/ic_action_recent_white"
app:showAsAction="always"
android:title="Undo" />
<item android:id="#+id/test"
android:icon="#drawable/ic_action_groups_white"
app:showAsAction="always"
android:title="Redo" />
</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" />