I am trying to add Menu item(action_collapse_expand) next to another menu item. Problem is that item is added to expandable list(three dots icon), but not as separate icon. I've just started learning Android and I would like to know what am I doing wrong.
<?xml version="1.0" encoding="utf-8" ?>
<!--For all properties see: http://developer.android.com/guide/topics/resources/menu-resource.html-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_collapse_expand"
android:icon="#drawable/drag_drop"
android:title="Collapse"
android:showAsAction="always|withText" />
<item android:id="#+id/add" android:showAsAction="never" android:title="add" android:icon="#drawable/Add">
<menu>
<item android:id="#+id/map" android:showAsAction="always|withText" android:title="map" android:icon="#drawable/Map" />
<item android:id="#+id/mapK" android:showAsAction="always|withText" android:title=mapK" android:icon="#drawable/MapK" />
</menu>
</item>
<item android:id="#+id/exit" android:showAsAction="always" android:title="Exit" android:icon="#drawable/Close" />
</menu>
Try replacing android:showAsAction with app:showAsAction.
Don't forget to add xmlns:app="http://schemas.android.com/apk/res-auto" under your menu tag.
Related
I can easily show icons in the menu for the subitems but what if I wanted to shows icon for the category menu items?
I tried like the code example below but it simply ignores android:icon parameter and displays just text.
First screenshot is what I get and the next one how I would like to get it looking.
<?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:showIn="navigation_view">
<item
android:icon="#drawable/ic_lists"
android:title="#string/lists">
<menu>
<item
android:id="#+id/nav_my_lists"
android:title="#string/my_lists" />
<item
android:id="#+id/nav_list_search"
android:title="#string/search_and_apply" />
<item
android:id="#+id/nav_list_applications"
android:title="#string/applications" />
<item
android:id="#+id/nav_list_banned"
android:title="#string/banned" />
</menu>
</item>
<item
android:icon="#drawable/ic_feedback"
android:title="#string/feedback">
<menu>
<item
android:id="#+id/nav_feedback_from_others"
android:title="#string/from_others" />
<item
android:id="#+id/nav_feedback_to_leave"
android:title="#string/i_need_to_leave" />
<item
android:id="#+id/nav_my_feedback"
android:title="#string/i_have_left" />
<item
android:id="#+id/nav_feedback_info"
android:title="#string/banned" />
</menu>
</item>
<item android:title="#string/logout" />
Question is, if there is menu icon property why it doesn't get displayed for top category menu items?
Use the Below Code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:icon="#drawable/ic_lists"
android:title="#string/lists">
<item
android:id="#+id/nav_my_lists"
android:title="#string/my_lists"
/>
<item
android:id="#+id/nav_list_search"
android:title="#string/search_and_apply" />
<item
android:id="#+id/nav_list_applications"
android:title="#string/applications" />
<item
android:id="#+id/nav_list_banned"
android:title="#string/banned" />
<item
android:icon="#drawable/ic_feedback"
android:title="#string/feedback"/>
<item
android:id="#+id/nav_feedback_from_others"
android:title="#string/from_others" />
<item
android:id="#+id/nav_feedback_to_leave"
android:title="#string/i_need_to_leave" />
<item
android:id="#+id/nav_my_feedback"
android:title="#string/i_have_left" />
<item
android:id="#+id/nav_feedback_info"
android:title="#string/banned" />
</group>
I'm working on a demo of DrawerLayout with fragments in android,I've implemented it but I want to add subitems to items when it is clicked it should populate the subitems just like an expandable listView. I have found some thread with expandable ListView,But I don't want it,Please suggest me how can I do it?
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Customer">
<menu
>
<item
android:id="#+id/nav_cust_mgmt"
android:icon="#drawable/ic_group"
android:title="Customer Management" >
</item>
<item
android:id="#+id/nav_cust_opn"
android:icon="#drawable/ic_user"
android:title="Customer Operation" />
<item
android:id="#+id/nav_cust_tkt"
android:icon="#drawable/ic_page"
android:title="Customer Tickets" />
</menu>
</item>
<item android:title="LCO">
<menu>
<item
android:id="#+id/nav_dashboard"
android:icon="#drawable/ic_home"
android:title="Dashboard" />
<item
android:id="#+id/nav_stm"
android:icon="#drawable/ic_stmnt"
android:title="Statement of Account" />
<item
android:id="#+id/nav_staf_mgmnt"
android:icon="#drawable/ic_grup"
android:title="Staff Management" />
<item
android:id="#+id/nav_my_tkts"
android:icon="#drawable/ic_page"
android:title="My Tickets" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_logout"
android:title="Logout" />
</menu>
</item>
</menu>
I want to add subcategories to this.Please help me.
You can create groups in menu file like this
<item
android:id="#+id/nav_menuItem9"
android:title="#string/str_drawer_group1">
<menu android:checkableBehavior="all">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_cust_mgmt"
android:icon="#drawable/ic_group"
android:title="Customer Management" >
</item>
...
</group>
</menu>
</item>
OR you can put expandable list view in drawer part and then handle everything manually. for more about this check this link
I am using Navigation Drawer menu without the header. The first item in the menu is too close to the top bar. How can I create margin for the first item? I am using this drawer menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Home"
android:id="#+id/nav_item_home"
android:icon="#drawable/ic_home_black_24dp"/>
<item android:title="Search Definitions"
android:id="#+id/nav_item_search"
android:icon="#drawable/ic_search_black_24dp"/>
<item android:title="About">
<menu>
<item android:title="About this app"
android:id="#+id/nav_item_about"
android:icon="#drawable/ic_info_black_24dp"/>
<item android:title="How to use this app"
android:id="#+id/nav_item_howtouse"
android:icon="#drawable/ic_perm_device_information_black_24dp"/>
</menu>
</item>
The outcome is this:
What I did as a workaround is to wrap the first item in a item menu with a blank title ... like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="">
<menu>
<item android:title="Home"
android:id="#+id/nav_item_home"
android:icon="#drawable/ic_home_black_24dp"/>
<item android:title="Search Definitions"
android:id="#+id/nav_item_search"
android:icon="#drawable/ic_search_black_24dp"/>
</menu>
</item>
<item android:title="About">
<menu>
<item android:title="About this app"
android:id="#+id/nav_item_about"
android:icon="#drawable/ic_info_black_24dp"/>
<item android:title="How to use this app"
android:id="#+id/nav_item_howtouse"
android:icon="#drawable/ic_perm_device_information_black_24dp"/>
</menu>
</item>
And then the outcome is exactly what I wanted:
Can someone please confirm if this workaround is the only way or is there any correct way of achieving the desired output.
Bit late, but any solution?
I've just played around in the xml:
<android.support.design.widget.NavigationView>
...
android:layout_marginTop="?attr/actionBarSize"
android:paddingTop="somePaddingValueInSp"
...
/>
I have a strange problem on an ActionBar I am setting up on a ListActivity.
Here is the XML I use to setup my menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:title="#string/search_filter"
android:icon="#drawable/ic_search"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_start_date"
android:title="#string/date_filter"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_end_date"
android:title="#string/date_filter"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_filters"
android:title="#string/filters"
android:icon="#drawable/ic_filter"
android:showAsAction="always|withText">
<menu>
<item
android:id="#+id/menu_ratings"
android:title="#string/ratings"
android:icon="#drawable/ic_star"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_emotions"
android:title="#string/emotions"
android:icon="#drawable/ic_smi2"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_categories"
android:title="#string/categories"
android:icon="#drawable/ic_folder"
android:showAsAction="always|withText" />
</menu>
<item
android:id="#+id/menu_search_order"
android:title="#string/search_order"
android:icon="#drawable/ic_search_order"
android:showAsAction="ifRoom|withText" />
</item>
</menu>
The menu items menu_search, menu_start_date, menu_end_date and the sub-menu menu_filters are working well but impossible to get the menu menu_search_order from the physical menu button (nothing happens when I press the button).
So I did a little test to see if the problem is not coming from the submenu, I changed the code and commented the submenu part:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:title="#string/search_filter"
android:icon="#drawable/ic_search"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_start_date"
android:title="#string/date_filter"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_end_date"
android:title="#string/date_filter"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_filters"
android:title="#string/filters"
android:icon="#drawable/ic_filter"
android:showAsAction="always|withText">
<!-- <menu>
<item
android:id="#+id/menu_ratings"
android:title="#string/ratings"
android:icon="#drawable/ic_star"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_emotions"
android:title="#string/emotions"
android:icon="#drawable/ic_smi2"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_categories"
android:title="#string/categories"
android:icon="#drawable/ic_folder"
android:showAsAction="always|withText" />
</menu> -->
<item
android:id="#+id/menu_search_order"
android:title="#string/search_order"
android:icon="#drawable/ic_search_order"
android:showAsAction="ifRoom|withText" />
</item>
</menu>
After this change, the menu works well and the option is displayed when I push the physical menu button.
So my question is: why is the submenu blocking my last menu to appear in the first case?
Thank you!
I still don't know why the code in my question is not working but I found the trick to finally make it work.
I just moved my optional menu (initially at the bottom) before the submenu in the XML file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_search"
android:title="#string/search_filter"
android:icon="#drawable/ic_search"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_start_date"
android:title="#string/date_filter"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_end_date"
android:title="#string/date_filter"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_search_order"
android:title="#string/search_order"
android:icon="#drawable/ic_search_order"
android:showAsAction="ifRoom|withText" />
<item
android:id="#+id/menu_filters"
android:title="#string/filters"
android:icon="#drawable/ic_filter"
android:showAsAction="always|withText">
<menu>
<item
android:id="#+id/menu_ratings"
android:title="#string/ratings"
android:icon="#drawable/ic_star"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_emotions"
android:title="#string/emotions"
android:icon="#drawable/ic_smi2"
android:showAsAction="always|withText" />
<item
android:id="#+id/menu_categories"
android:title="#string/categories"
android:icon="#drawable/ic_folder"
android:showAsAction="always|withText" />
</menu>
</item>
</menu>
The order here is not important because this menu is the only one I need to see through the physical menu button.
So after so many tries I found a way how to make those items display everywhere correctly. So the trick is to put all items to group element with some id and also android:checkableBehavior="single" , this is only way I was able to get it to render as defined in xml. If I remove id from first group last two items get merged to previous menu sub items. Quite buggy android menu implementation. It should be reported somewhere, but hopefully someone else will do it.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/signup_1_identification_group"
android:checkableBehavior="single">
<item
android:id="#+id/dopkkpokadad"
android:title="#string/signup_1_identification" />
</group>
<item
android:id="#+id/kokopj"
android:title="#string/signup_6_nationality">
<menu android:id="#+id/signup_6_nationality_menu">
<group
android:id="#+id/signup_6_nationality_group"
android:checkableBehavior="single">
<item
android:id="#+id/dadceqpovjopeqad"
android:title="Hlavna" />
<item
android:id="#+id/cionno"
android:title="Dalsia" />
<item
android:id="#+id/dkbvyukbadad"
android:title="Materinsky Jazyk" />
</group>
</menu>
</item>
<group
android:id="#+id/signup_last_group"
android:checkableBehavior="single">
<item
android:id="#+id/dafeqpokopqdad"
android:title="#string/signup_7_religion" />
<item
android:id="#+id/dakopefhpoiueqdad"
android:title="#string/signup_8_finalization" />
</group>
</menu>
This is how it is rendered correctly from this xml:
When I remove id from fist group I get back wrong behaviour, last two items gets merged, in some cases they will disappear or other weird stuff happens depending on other layout changes, I believe android menu implementation is just buggy...
In one of my Android projects, I am creating a context menu from a menu folder inside res. It is as below.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/help"
android:title="Help" />
<item android:id="#+id/keyboard"
android:title="Keyboard" />
<item android:id="#+id/exit"
android:title="Exit" />
</menu>
But I want an image to the background of every item. How can I do this?
Try this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/help"
android:title="Help"
android:icon="#drawable/ic_1/>
<item android:id="#+id/keyboard"
android:title="Keyboard"
android:icon="#drawable/ic_2" />
<item android:id="#+id/exit"
android:title="Exit"
android:icon="#drawable/ic_3/>
</menu>
Use styles in your app. Then you can change a default style of Widget.ListView. Note this also changed a style of dialogs with ListView. More details see in this answer.