Appcompat toolbar showAsAction doesn't work properly - android

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.

Related

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 display menu item in action bar in android

menu item is not appearing in action bar. I am trying this code on API 19, kitkat 4.4
using this XML code.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="Sign out"
android:id="#+id/iSignout"
android:icon="#drawable/signout"
android:orderInCategory="100"
android:showAsAction="always"/>
<item
android:title="My Profile"
android:id="#+id/iEditProfile"/>
<item
android:title="Edit Profile"
android:id="#+id/iMyProfile"/>
</menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_bar_menu, menu);
return true;
}
Try this:
<?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:title="Sign out"
android:id="#+id/iSignout"
android:icon="#drawable/signout"
android:orderInCategory="100"
app:showAsAction="always"/>
<item
android:title="My Profile"
android:id="#+id/iEditProfile"/>
<item
android:title="Edit Profile"
android:id="#+id/iMyProfile"/>
</menu>
Replace android:showAsAction="always" with app:showAsAction="always"
Try
android:showAsAction="ifRoom"
How to display settings menu on click of settings icon? settings-menu-on-click-of-settings-icon/41716117#41716117

Android: Action button icons always appear in overlay

Action items always appear in overlay. Here is the code:
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">
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:icon="#drawable/ic_event_black_18dp"
android:orderInCategory="1"
app:showAsAction="always" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
and in the main activity:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
Shouldn't the first item appear on the action bar because of "app:showAsAction="always" ?
Use this inside your item ,to display in Action bar
app:showAsAction="always"
When you dont want to display item in Action bar
app:showAsAction="never"
Try this piece of code , instead of your creating Option Menu
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
If it is for fragments
custom namespace will prevent showAsAction from showing. so
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:icon="#drawable/ic_event_black_18dp"
android:orderInCategory="1"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_add" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>

Item does not appear on menu [duplicate]

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;
}

onCreateOptionsMenu doesn't set icons or options in the ActionBar the first time the activity is loaded in Ice Cream Sandwich

I'm using the normal onCreateOptionsMenu with a MenuInflater to load my menu from xml for the action bar and it works fine on Honeycomb but for some reason on Ice Cream Sandwich it doesn't load the first time, subsequent trips to the activity will yield the expected items on the action bar.
Here's my onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.e("test", "createmenu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map_menu, menu);
// if (presets.size() == 0)
// menu.getItem(4).setEnabled(false);
return true;
}
And here's my menu xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/info"
android:icon="#drawable/info"
android:title="Info"
android:showAsAction="always" />
<item android:id="#+id/preset"
android:title="Display Preset Option"
android:icon="#drawable/presets"
android:showAsAction="always" />
<item android:id="#+id/favorites"
android:title="Favorites"
android:icon="#drawable/favorites"
android:showAsAction="always" />
<item android:id="#+id/add_favorite"
android:title="Add Favorite"
android:icon="#drawable/plus"
android:showAsAction="always" />
<item android:id="#+id/location"
android:title="Current Location"
android:icon="#drawable/location"
android:showAsAction="always" />
<item android:id="#+id/return_map"
android:title="Return to Original Map"
android:showAsAction="ifRoom" />
<item android:id="#+id/raster"
android:title="Show Raster"
android:icon="#drawable/raster"
android:showAsAction="always" />
<item android:id="#+id/query"
android:title="Queries"
android:icon="#drawable/queries"
android:showAsAction="always" />
<item android:id="#+id/displayNotes"
android:title="Hide Notes" />
<item android:id="#+id/displayPhotos"
android:title="Display Photos"
android:visible="false" />
<item android:id="#+id/add_note"
android:title="Add Note"/>
<item android:id="#+id/help"
android:title="Help"/>
<item android:id="#+id/email"
android:title="Email Map"/>
</menu>
Anyone seen anything like this before?
There was a case where onPrepareOptionsMenu(menu) was returning false instead of super.onPrepareOptionsMenu(menu) and fixing that has solved my problem. I guess something has changed as of Ice Cream Sandwich. Either way, if you're experiencing the same problem as I had make sure you're returning super.onPrepareOptionsMenu(menu) and not false.
This is the right way to do it:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuColores: /your code here
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuColores" android:alphabeticShortcut="k"
android:icon="#drawable/palette" /> </menu>
I'm developing an app and this is a snippet of my code.. it works just fine! sometimes I have to clean the project when I change the item's image, it may not recognize the image otherwise

Categories

Resources