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!
Related
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>
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>
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.
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" />
I wanted to create a simple menu for an app. Here is the XML code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<item android:id="#+id/new_game"
android:icon="#drawable/magnify"
android:title="New Game"
android:showAsAction="ifRoom"
android:layout_height="wrap_content"/>
<item android:id="#+id/help"
android:icon="#drawable/magnify"
android:title="Help" />
</menu>
But when I click on the design Tab, it gives an error saying that
"Rendering Problems
The following classes cannot be found
-item(Fix build path, Edit in XML)
-menu(Fix build path, Edit in XML)"
What should I do?
Thanks in advance
It looks you have some attributes in your menu that aren't valid - they're layout attributes, not menu attributes.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/magnify"
android:title="New Game"
android:showAsAction="ifRoom"
/>
<item android:id="#+id/help"
android:icon="#drawable/magnify"
android:title="Help"
android:showAsAction="ifRoom"
/>
</menu>