Item does not appear on menu [duplicate] - android

This question already has answers here:
Show Menu item always in support action bar
(5 answers)
Closed 7 years ago.
I created an item "+" that i want to appear next to the three dots menu on the top left.
so this is the xml:
<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="com.example.ali.test1.MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="1" app:showAsAction="never" />
<item
android:id="#+id/action_cart"
android:title="+"
android:orderInCategory="2"
android:showAsAction="always"/>
</menu>
now I'm getting the "+" item inside the action_settings and not on the side
I did check "Show Menu item always in support action bar" but it did not help
any help ?

first remove android:orderInCategory and then put action_cart before action_settings
and try to use only android:showAsAction instead of app:showAsAction
so your xml should look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.ali.test1.MainActivity">
<item
android:id="#+id/action_cart"
android:title="+"
android:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>

try changing always to ifRoom and take out android:orderInCategory="2"
<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="com.example.ali.test1.MainActivity">
<item
android:id="#+id/action_cart"
android:title="+"
app:showAsAction="ifRoom"/>
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="1" app:showAsAction="never" />
check you are using that menu in the activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.**themenuyouareusing**, menu);
return true;
}

Related

Appcompat toolbar showAsAction doesn't work properly

My device allows to show up to 3 icons in the taskbar, if I exceed this number all the icons are hidden even if I have showAsAction="always" or showAsAction="ifRoom".
This is my code:
menu.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<group android:id="#+id/groupActions">
<item
android:id="#+id/action_admin"
android:orderInCategory="0"
android:title="#string/buttonAdmin"
app:showAsAction="always" />
<item
android:id="#+id/action_readMode"
android:orderInCategory="1"
android:title="#string/buttonReadModeOn"
android:icon="#drawable/ic_read_mode_24dp"
app:showAsAction="always" />
</group>
<group android:id="#+id/groupActions">
<item
android:id="#+id/action_schedule"
android:orderInCategory="2"
android:title="#string/buttonSchedule"
android:icon="#drawable/ic_today_24dp"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_report"
android:orderInCategory="3"
android:title="#string/buttonReport"
android:icon="#drawable/ic_list_check_24dp"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_logout"
android:orderInCategory="4"
android:title="#string/buttonLogout"
app:showAsAction="never" />
</group>
<group android:id="#+id/groupInfo">
<item
android:id="#+id/action_help"
android:orderInCategory="5"
android:title="#string/buttonHelp"
android:visible="false"
app:showAsAction="never" />
<item
android:id="#+id/action_contact"
android:orderInCategory="6"
android:title="#string/buttonContact"
app:showAsAction="never" />
<item
android:id="#+id/action_about"
android:orderInCategory="7"
android:title="#string/buttonAbout"
app:showAsAction="never" />
</group>
</menu>
Here the code on MyActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
MenuCompat.setGroupDividerEnabled(menu, true);
this.menu = menu;
MenuItem item = menu.findItem(R.id.action_readMode);
if (item != null) {
tintReaderModeIcon(item);
}
return super.onCreateOptionsMenu(menu);
}
My idea is to show the two icons with showAsAction="always" and the icon that has showAsAction="ifRoom" with the highest priority.
This is the desired result:
And this is what I get:
Please help to clear this out.
EDIT: Thanks to Denis95's response I have managed to solve half of the problem. Now the icons with showAsAction="true" are displayed correctly.
As i can see from the resource file, you're grouping 5 items together. Try to put the items you want to always show on the AppBar outside that group. It should do the trick.

How do i make sure the Menu Item is always on the topbar?

the 2 menu item connect and disconnect coded such that only 1 of them shows at a time.
I want to make it on the top bar, and not under the ... button.
following is my menu xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_refresh"
android:checkable="false"
android:orderInCategory="1"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_connect"
android:icon="#android:color/holo_blue_bright"
android:orderInCategory="100"
android:title="#string/menu_connect"
app:showAsAction="ifRoom|withText" />
<item
android:id="#+id/menu_disconnect"
android:orderInCategory="101"
android:title="#string/menu_disconnect"
app:showAsAction="ifRoom|withText" />
You can try:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_refresh"
android:checkable="false"
android:orderInCategory="1"
app:showAsAction="always" />
<item
android:id="#+id/menu_connect"
android:icon="#android:color/holo_blue_bright"
android:orderInCategory="100"
android:title="#string/menu_connect"
app:showAsAction="always" />
<item
android:id="#+id/menu_disconnect"
android:orderInCategory="101"
android:title="#string/menu_disconnect"
app:showAsAction="always" />
I hope it will help your problem!
If you still have same problem even you set app:showAsAction="always", you should check onCreateOptionsMenu. Please try this if you're creating menu differently, it will help you.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
Have you tried setting...
app:showAsAction="always"
... in the item you want to be shown in the top bar (app bar) always?
EDIT:
or...
app:showAsAction="always|withText"
if yout want to show the title too.

How to hide menu elements in Android?

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().

Menu items not shown in menu bar

I have the same problem as posted many times like here or here. I define a menu item which shows up in the preview in AndroidStudio:
But when I run the app on my phone the icon (a png image) is not visible, and there is a lot of space available. However, this 'Add' option shows up in the Options menu (to the very right; together with 'Srttings'). Here is my menu.xml:
<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=".MainActivity">
<item android:id="#+id/action_favourite"
android:icon="#mipmap/ic_add"
android:title="#string/menu_add"
android:showAsAction="always"/>
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
I tried the suggestions I could find, but none of them solved my problem. My phone is a LG G3. How can I solve this problem?
Additional information: onCreateOptionsMenu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Just use app:showAsAction="always" and xmlns:app="http://schemas.android.com/apk/res-auto" then it will show.
<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=".MainActivity">
<item android:id="#+id/action_favourite"
android:icon="#mipmap/ic_add"
android:title="#string/menu_add"
app:showAsAction="always"/>
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
You're probably using the support library which is dependent on the app namespace. If in doubt, just add the same property twice (android: and app: namespaces).
In onCreate() add setHasOptionsMenu(true)
And you are probably inflating the wrong menu file.

Action Bar menu - Submenu showing wrong items.

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>

Categories

Resources