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.
Related
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"
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.
I am using v7 Support library and using app namespace in the menu_main.xml file. Even then, the action is never displayed in the action bar but in the overflow bar. This happens even when I use app:showAsAction="always"
menu_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
<item android:id="#+id/action_create_order"
android:title="#string/action_create_order"
android:orderInCategory="1"
app:showAsAction="always"
android:icon="#drawable/add_box_black_icon"
/>
</menu>
The order of the items is important - swap the create order and settings if you want to achieve your original idea.
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;
}
I followed the steps according to the their official website. Still it doesn't show up items on the actionbar. I also updated the theme name on manifest file.
Any help?
Your question is so general but I think you need to change your menu xmls. They are under res/menu. If the name of your class is main, then menu should be in menu_main.xml. You should change app:showAsAction to ifRoom or always.
<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_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="always" android:icon="#drawable/ic_action_important" />
</menu>