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().
Related
I'm using Navigation Drawer. I've used a spinner layout in a menu item, but it looks bad. I'm trying to have a view like this, but IDK how.
So when I press PRODUCT, a dropdown submenu appears.
I've tried this code to do so but it's not working.
<?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:showIn="navigation_view">
<group android:checkableBehavior="single"
>
<item
android:id="#+id/nav_home"
android:checkable="true"
android:icon="#drawable/ic_outline_home_24"
android:title="#string/home" />
<item
android:id="#+id/nav_products"
android:checkable="true"
android:visible="true"
android:actionLayout="#android:layout/simple_spinner_dropdown_item"
android:icon="#drawable/ic_outline_shopping_bag_24"
app:showAsAction="collapseActionView"
android:title="#string/products" >
<menu>
<item
android:id="#+id/fire_alam"
android:title="#string/fire_alarms"
android:checkable="true"
app:showAsAction="never"
/>
<item
android:id="#+id/fire_fighting"
android:checkable="true"
app:showAsAction="never"
android:title="#string/fire_fighting"/>
</menu>
</item>
<item
android:id="#+id/nav_serv"
android:checkable="true"
android:icon="#drawable/ic_outline_room_service_24"
android:title="#string/services" />
</group>
</menu>
You can use ExpandableListView
Check:- https://www.journaldev.com/9942/android-expandablelistview-example-tutorial
Also you can use it in NavigationView
Check:-
https://www.journaldev.com/19375/android-expandablelistview-navigationview
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.
My main file:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings">
<menu>
<item
android:id="#+id/menu_adhoc1"
android:icon="#drawable/icon_custom"
android:title="#string/menuitem2_3"/>
<item
android:id="#+id/cPanel1"
android:icon="#drawable/icon_cpanel"
android:title="#string/menuitem2_5"/>
<item
android:id="#+id/tutorial1"
android:icon="#drawable/icon_tutorial"
android:title="#string/menuitem2_4"/>
</menu>
</item>
</menu>
What I am trying to achieve is that place three more options under the menu, however I get only one option under menu labelled as "Settings", clicking on this leads to the desired result of getting three options under the menu. Where am I going wrong, any hints?
The problem is that you nested a <menu> containing <item>s inside another <item>.
As per the Menu documentation, this adds your second menu as a submenu of the parent item.
What it sounds like you are looking for is all the items being withing the same menu, like so:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings" />
<item
android:id="#+id/menu_adhoc1"
android:icon="#drawable/icon_custom"
android:title="#string/menuitem2_3"/>
<item
android:id="#+id/cPanel1"
android:icon="#drawable/icon_cpanel"
android:title="#string/menuitem2_5"/>
<item
android:id="#+id/tutorial1"
android:icon="#drawable/icon_tutorial"
android:title="#string/menuitem2_4"/>
</menu>
When I do like this it works:
<item
android:id="#+id/menu_show_location"
android:title="#string/menu_show_location"
android:icon="#android:drawable/ic_dialog_map"
app:showAsAction="always"/>
But if I take icon from app drawable:
<item
android:id="#+id/menu_show_location"
android:title="#string/menu_show_location"
android:icon="#drawable/ic_action_place"
app:showAsAction="ifRoom"/>
the menu item doesn't show up nor in the menu nor in the action bar either.
Full 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/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item
android:id="#+id/menu_show_location"
android:title="#string/menu_show_location"
android:icon="#android:drawable/ic_dialog_map"
app:showAsAction="always"/>
<!--android:icon="#drawable/ic_action_place"-->
</menu>
What can be a problem?
Try this,
<item
android:id="#+id/menu_show_location"
android:title="#string/menu_show_location"
<!--android:icon="#android:drawable/ic_dialog_map"-->
<!-- need to add an order-->
android:orderInCategory="200"
android:icon="#drawable/ic_action_place"
app:showAsAction="always"/>
In situations like that changing icon color or app theme may helps ))
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" />