I have to following menu 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/menu_add"
android:contentDescription="#string/menu_item_add"
android:icon="#drawable/ic_add"
android:title="#string/menu_item_add"
app:showAsAction="ifRoom" />
</menu>
But I want to create a test where icon is null and run it .
Any idea how to mock the menu item without icon?
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 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.
I have menu resource like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/nav_drawer">
<item android:title="my profile" android:icon="#drawable/ic_user"/>
<item android:title="my_msg" android:icon="#drawable/ic_action_email"/>
</group>
</menu>
how can i bind this xml to the my_list in main_activity.xml
<ListView
android:id="#+id/my_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
I think you are trying to achieve a contextual Menu in your activity or fragment. Using context menu is the solution for you according to me.
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.
I need to add master on/off buttons in to sub menus in my action bar menus and small description under the title.Also I need to add heading for sub menus. I don't have idea how to do it. Do I have to use separate layout for each sub menu or else is there any way to fix using current menu.xml file?
This is what I want to do :
This is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_settings"
android:showAsAction="always"
android:title="#string/action_settings"
android:icon="#drawable/settings">
<menu >
<item
android:id="#+id/title"
android:title="HEADING"/>
<item
android:id="#+id/theme"
android:title="theme"
android:showAsAction="never"
android:orderInCategory="1"
android:icon="#drawable/ic_action2"
android:actionLayout="#layout/switch_layout"/>
<item
android:id="#+id/volunm"
android:title="Volumn"
android:orderInCategory="2"
android:showAsAction="never"
android:icon="#drawable/ic_action1"/>
</menu>
</item>
</menu>
This is my switch_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="#+id/switchForsetting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="on/off" />
</RelativeLayout>
But I'm not getting Switch button.
Use Action provider for this. It will be easier. here