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

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.

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>

Android Action bar not showing Icons

How can i display Icons in Actions bar, here is my code,
<?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/search_icon"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:orderInCategory="0"
android:title="Search" >
</item>
</menu>
and
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater mif = getMenuInflater();
mif.inflate(R.menu.custom_action_bar,menu);
return super.onCreateOptionsMenu(menu);
}
Thanks for Help !
If using app compat use app:showAsAction
<item android:id="#+id/search_icon"
android:icon="#drawable/ic_action_search"
app:showAsAction="always"
android:orderInCategory="100"
android:title="Search" >
</item>
In Manifest.xml File
<activity android:name="your activity name"
android:theme="#style/Theme.Holo.Light"></activity>

android setting up the Action bar items

The below layout not displaying search icon only the another icon is displaying and the search comes as the drop down menu of other icon.I want it as separate icon in the action bar
<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.diveintojava1.MainActivity" >
<item android:id="#+id/search"
android:title="search"
android:icon="#drawable/search1"
android:actionViewClass="android.support.v7.widget.SearchView"
android:showAsAction="collapseActionView|ifRoom"
/>
<item android:id="#+id/file"
android:title="drop"
android:icon="#drawable/moremenu" >
<menu>
<item android:id="#+id/create_new"
android:title="java" />
<item android:id="#+id/open"
android:title="rate us" />
</menu>
</item>
</menu>
And below is the java code of implementing menu
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
Try this, see more here : http://developer.android.com/guide/topics/ui/actionbar.html and here http://sourceoncloud.wordpress.com/2013/07/26/actionbar-compact/
<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"
xmlns:appcompact="http://schemas.android.com/apk/res-auto"
tools:context="com.example.diveintojava1.MainActivity" >
<item android:id="#+id/search"
android:title="search"
android:icon="#drawable/search1"
android:actionViewClass="android.support.v7.widget.SearchView"
appcompact:showAsAction="collapseActionView|ifRoom"
/>
<item android:id="#+id/file"
android:title="drop"
android:icon="#drawable/moremenu" >
<menu>
<item android:id="#+id/create_new"
android:title="java" />
<item android:id="#+id/open"
android:title="rate us" />
</menu>
</item>
</menu>
android:showAsAction="collapseActionView|ifRoom" Change ifRoom to always
Change this:
android:showAsAction="collapseActionView|ifRoom"
into this:
android:showAsAction="always"

Categories

Resources