app:showAsAction="ifRoom" not working - android

I'm new to Android and am having a problem that seems to be pretty popular. None of the solutions are working for me so I can't figure out what's wrong.
I've got a Menu with 2 items in it (Search, Shop By), and I want Search to appear in the Action Bar. However, app:showAsAction="ifRoom" is not moving Search as it still appears in the overflow.
Here is the XML for the menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/res-auto">
<item android:id="#+id/shop_by"
android:icon="#drawable/ic_shop_by"
android:title="#string/shop_by_title"/>
<item android:id="#+id/search"
android:icon="#drawable/ic_search"
android:title="#string/search"
app:showAsAction="ifRoom"/>
</menu>
Here is where I override onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
What am I missing?
I tried app:showAsAction="ifRoom|withText" and got the same behavior.
I tried android:showAsAction="ifRoom"and got an error.
EDIT:
SOLVED!
The namespace for app should look like this:
xmlns:app="http://schemas.android.com/apk/res-auto"

You need to try adding orderInCategory attribute. Something like 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:id="#+id/shop_by"
android:icon="#drawable/ic_shop_by"
android:title="#string/shop_by_title"
android:orderInCategory="100"/>
<item android:id="#+id/search"
android:icon="#drawable/ic_search"
android:title="#string/search"
android:orderInCategory="99"
app:showAsAction="ifRoom"/>
</menu>
Use app:showAsAction="always" if you want to always show the menu item.
Note: the answer is updated from xmlns:app="http://schemas.android.com/res-auto" to xmlns:app="http://schemas.android.com/apk/res-auto"
kudos to #MikeM

If you are using 
android.support.v7.app.Activity
 the xml should be like yours.
And if you are using 
android.app.Activity
 please use:
android:showAsAction="ifRoom|withText"
instead of
app:showAsAction="ifRoom|withText"

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

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

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.

getActionView from MenuItem returns null if wrong order

As the title says, I can't figure it out. I'm NOT using compat support library. Using android:Theme.Holo.Light.DarkActionBar
This is my action_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/action_search"/>
</menu>
This is my activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_menu, menu);
mi = menu.findItem(R.id.action_search);
search = (SearchView)mi.getActionView();
search.setOnQueryTextListener(getOnQueryTextListener());
new SearchTask().execute(getIntent().getStringExtra("query"));
return true;
}
mi is a MenuItem. search is a SearchView.
If the order of declaration of the items in action_menu.xml is as written, getActionView returns null.
If the order is inverted, it works. Why?
try like 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:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
android:title="#string/action_search"/>
</menu>
hope it will help you

Categories

Resources