Android 3.0: Create a custom menu with submenu - android

I am working with Android 3.0.
I am trying to build a custom menu in the action bar that has sub-menus in it.
Anybody has the XML hierarchy that should be written?

<item> elements can contain <menu> elements. I've only tested this on ICS but it looks like it should work on Honeycomb as well.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_dropdown"
android:title="More"
android:showAsAction="always">
<menu>
<item android:id="#+id/menu_one" android:title="One" />
<item android:id="#+id/menu_two" android:title="Two" />
</menu>
</item>
</menu>

Related

Error when Adding Menu to App - Xamarin c# Visual Studio

I am trying to inflate an XML Menu inside my App, but i get the following error when I try to Deploy the App on an Emulator:
Unhandled Exception:
Java.Lang.RuntimeException: Unexpected end of document occurred
Also my XML is located in Resources/menu/main
Inflate Menu Code:
XML Code:
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/mnuAppLogo"
android:title="logoHere"/>
<item android:id="#+id/mnuAppName"
android:title="App Name"/>
<menu>
<item android:id="#+id/submenuHelp"
android:title="Help" />
<item android:id="#+id/submenuExit"
android:title="Exit" />
</menu>
</menu>
Why is this?
Let me know if you need more code...
Thanks in advance.
UPDATE 1:
I want to make the logoHere and App Name appear in spots 1 and 2 with the other 2 Help and Exit inside the Menu.
To create a "submenu", the elements must be included within an item element:
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mnuAppLogo" android:title="logoHere" />
<item android:id="#+id/mnuAppName" android:title="App Name">
<menu>
<item android:id="#+id/submenuHelp" android:title="Help" />
<item android:id="#+id/submenuExit" android:title="Exit" />
</menu>
</item>
</menu>
How do I make it so that the First 2 (logoHere and App Name) are not in the "Hamburger Menu" and just on the Action Bar? The Help and Exit however will be inside the Menu.
You can use showAsAction="never" to always place the menu item in the overflow menu and showAsAction="ifRoom" to display it as an action bar button IF there is room for it.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/mnuAppLogo" showAsAction="ifRoom" android:title="logoHere" />
<item android:id="#+id/mnuAppName" showAsAction="ifRoom" android:title="App Name" />
<item android:id="#+id/submenuHelp" showAsAction="never" android:title="Help" />
<item android:id="#+id/submenuExit" showAsAction="never" android:title="Exit" />
</menu>

when i set icon to drawer menu then the drawer menu become slow in android

I'm new in Android
when i set icon in drawer_menu.xml code then after when i open the
navigation drawer menu it open slowly why?
If any one have answer to this question or ever faced this type of
issues then plz reply to this question.
Here is my drawer_menu.xml code.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/home_id"
android:icon="#drawable/home"
android:title="Home"></item>
<item
android:id="#+id/id_setting"
android:icon="#drawable/setting"
android:title="Settings"></item>
</group>
<item android:title="Others">
<menu>
<item
android:id="#+id/notifications"
android:icon="#drawable/noti"
android:title="Notifications"></item>
<item
android:id="#+id/about_us"
android:icon="#drawable/aboutus"
android:title="About us"></item>
</menu>
</item>
</menu>
What is the size of icon?
use 300*300

Top margin for first item in navigation drawer menu in Android Studio

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"
...
/>

Couldn't show items with AppCompat library

I am having some troubles to show items in the ActionBar with AppCompat.
This code works on a normal actionbar
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item1"
android:showAsAction="always"
android:title="Se connecter"
android:visible="true">
</item>
</menu>
But with the AppCompat library the item isn't shown..
What should I do ?
Thanks in advance :)
showAsAction is not in the android xml namespace for API < 11 try something like the following:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_refresh"
android:title="#string/refresh"
yourapp:showAsAction="always"
android:icon="#drawable/ic_action_refresh" />
</menu>
Note that I've added xmlns:yourapp="http://schemas.android.com/apk/res-auto in menu attributes and changed the namespace of showAsAction from android to yourapp.
More reading here: http://developer.android.com/guide/topics/ui/actionbar.html

How do I change the orientation and layout of buttons for Android Menu?

I want to change how the items are oriented in the menu of my app. Currently I have two buttons with one stacked on the other. How can I place it side by side?
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:orderInCategory="100"
android:title="#string/menu_settings"
android:actionLayout="#layout/main"/>
<item
android:id="#+id/menu_help"
android:title="#string/menu_help"
android:actionLayout="#layout/main"/>
</menu>
Sorry for poor formatting, I am new to stack overflow.
Try something like
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item1"
android:icon="#drawable/item1icon"
android:title="#string/itemString"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
It's weird that your buttons are stacked on top of each other. You should post your XML

Categories

Resources