error with Submenu in XML file - android

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<item
android:id="#+id/item1"
android:title="Main Menu"
/>
<item
android:id="#+id/takesurvey"
android:title="Take Survey"
/>
<item
android:id="#+id/viewstats"
android:title="View Statistics"
/>
<item
android:id="#+id/changesort"
android:title="Change Sorting Order "
<menu>
<item
android:id="#+id/create_new"
android:title="#string/create_new" />
<item
android:id="#+id/open"
android:title="#string/open" />
</menu>
/>
<item
android:id="#+id/menuexit"
android:title="Exit from program "
/>
</item>
Hello , this seems to be a simple problem.I am trying to create a submenu ( for the change sort option ) in my XML file for android. However , I am geting an error :Element type "item" must be followed by either attribute specifications, ">" or "/>". Any ideas?

Your XML is a mess
You've got this open tag, with no closing tag:
<item >
You then have this, which is a partial tag:
<item
android:id="#+id/changesort"
android:title="Change Sorting Order "
I suggest you reformat your XML to indent every sub-tag, and make sure every tag either ends with /> or a closing tag pair.

Related

Creating a menu layout in XML android studio

I am getting an unknown class error while creating a menu layout.XMl in android studio. Might someone please help me to solve this issues?
The compiler does not recognize the attribute "item".
<item android:id=”#+id/about”
android:title=”About” />
<item android:id=”#+id/help”
android:title=”Help” />
You need to add a menu before them and put in this directory res/menu/
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/about"
android:icon="#drawable/ic_about"
android:title="about" />
<item android:id="#+id/help"
android:icon="#drawable/ic_ help"
android:title="help"/>
</menu>

How to display menu icons for the menu categories in the Navigation Drawer?

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>

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>

Toolbar icons are not showing

I'm trying to use Toolbar in my app, I was able to follow android tutorial to the point of displaying these items as icons.
My Items shows as a sub menu of "..." on the right but no icon is shows. I tried using android:showAsAction="always" but I keep getting errors about issues with a name space and that I should use res-auto. When I tried that break my inflator of the menu and I was getting some strange title errors even though I had these tags defined.
Is there a way to display some icons on a toolbar and how can I force it with showAsAction=always without throwing errors.
Thank you
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/favorite_ico"
android:icon="#mipmap/ic_launcher"
android:title="Item 1"
android:showAsAction="ifRoom" />
<item
android:id="#+id/settings_ico"
android:title="Item 2" />
</menu>
if you are trying to use showAsAction="always"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/favorite_ico"
android:icon="#mipmap/ic_launcher"
android:title="Item 1"
app:showAsAction="always" />
<item
android:id="#+id/settings_ico"
android:title="Item 2" />
</menu>
you need to import app not android
Please modify your code to this
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/favorite_ico"
android:icon="#mipmap/ic_launcher"
android:title="Item 1"
app:showAsAction="ifRoom" />
<item
android:id="#+id/settings_ico"
android:title="Item 2" />
</menu>
showAsAction should be from support library. for this purpose you need to use app:showAsAction. this need appNs
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/favorite_ico"
android:icon="#mipmap/ic_launcher"
android:title="Item 1"
app:showAsAction="ifRoom">
</item>
<item android:id="#+id/settings_ico"
android:title="Item 2">
</item>
</menu>

LevelListDrawable with an empty case

Is it possible in Android to give to a LevelListDrawable an empty case? "Empty" is like "No Image". For instance, take a look at the LevelListDrawable here below:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/face_1"
android:maxLevel="1" />
<item
android:drawable="#drawable/face_2"
android:maxLevel="2" />
<item
android:drawable="#drawable/face_3"
android:maxLevel="3" />
<item
android:drawable="#drawable/face_4"
android:maxLevel="4" />
<item
android:drawable="#drawable/face_5"
android:maxLevel="5" />
<item
android:drawable="#drawable/face_6"
android:maxLevel="6" />
</level-list>
It is a Dice with its 6 faces. I'd like to have an empty face too. A NULL case, or something like that.
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable=""
android:maxLevel="0" />
<item
android:drawable="#drawable/face_1"
android:maxLevel="1" />
<item
android:drawable="#drawable/face_2"
android:maxLevel="2" />
<item
android:drawable="#drawable/face_3"
android:maxLevel="3" />
<item
android:drawable="#drawable/face_4"
android:maxLevel="4" />
<item
android:drawable="#drawable/face_5"
android:maxLevel="5" />
<item
android:drawable="#drawable/face_6"
android:maxLevel="6" />
</level-list>
Currently I simulate this behavior like this:
ImageView image = new ImageView(context);
image.setImageDrawable(getResources().getDrawable(R.drawable.dice));
image.setImageLevel(3);
if(somethingHappened){
image.setImageDrawable(null);
}
But after that I have to call image.setImageDrawable(getResources().getDrawable(R.drawable.dice)); another time instead of changing the image level. Thank you.
minLevel is 0 by default, so if you want to exclude face_1 from showing for level=0 add it explicitly
<item
android:drawable="#drawable/face_1"
android:minLevel="1"
android:maxLevel="1" />
The problem is that now face_2 will be shown (because it's matching [0, 2]), but if you add min and max for all of the faces with the same values each, they'll play nicely.
See android.graphics.drawable.LevelListDrawable.LevelListState#indexOfLevel for more.
Alternatively I saw a magic value somewhere:
<item
android:drawable="#null"
android:maxLevel="0" />
But this will likely throw an NPE or exception from LevelListDrawable#inflate:
<item> tag requires a 'drawable' attribute or child tag defining a drawable

Categories

Resources