Item with app:showAsAction not showing
Things you should always check when you want to use action bar are
1) Extend ActionBarActivity
instead of Activity
public class MainMenu extends ActionBarActivity{
2) Have the right style selected as defined at manifest
Manifest
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
Style
<style name="AppTheme"
parent="Theme.AppCompat.Light.DarkActionBar">
</style>
3) Select the right title for showAsAction
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:**yourapp**="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
**yourapp**:showAsAction="ifRoom" />
...
</menu>
This is what most people get wrong
4) Define your Menu
in Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
If you do all the following your action bar should work.
Then you should add the onClickListener
for every position...
Android appcompat actionbar menu item showAsAction not working
Whew, thanks for your help guys but I managed to figure it out. It wasn't a problem with the xml, it was a problem with the onCreateOptionsMenu function.
I was using this
new MenuInflater(getApplication()).inflate(R.menu.activity_wentry_editor, menu);
instead of this
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_wentry_editor, menu);
Not entirely sure why this works but it does.
Items not showing in the ActionBar with showAsAction=“always”
try android:showAsAction
instead of app:showAsAction
. Or if you're using the appcompat_v7
backport, use both android:showAsAction
and app:showAsAction
(Thanks to Commonsware).
app:showAsAction=ifRoom not working
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
Appcompat toolbar showAsAction doesn't work properly
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.
Android menu item set to always be shown is not being shown
Instead of app:showAsAction change it to android:showAsAction as shown:
<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="myAppID.MainActivity" >
<item android:id="@+id/action_one"
android:title="One"
android:icon="@drawable/your_icon"
android:showAsAction="always" />
<item android:id="@+id/action_two"
android:title="Two"
android:showAsAction="never" />
<item android:id="@+id/action_three"
android:title="Three"
android:showAsAction="never" />
</menu>
Hope it works.
ActionBar showAsAction not working
app:showAsAction
is used with AppCompatActivity. You must either extend that class and use AppCompat
or use android:showAsAction
.
app:showAsAction ifRoom is not working on appcompat action bar
Please Try to use
android:showAsAction="ifRoom|withText"
instead of app:showAsAction="ifRoom|withText"
Action Items not showing in ActionBar with showAsAction=ifRoom
I found the problem. There was an error in the menu .xml file. In fact, I've added a new namespace:
xmlns:app="http://schemas.android.com/apk/res-auto"
But then, I still refer the property as if it belongs to the android
namespace:
android:showAsAction="ifRoom"
The right way to refer this property is to use:
app:showAsAction="ifRoom"
Cause it belongs to the namespace app
.
Here is the relevant part in documentation:
If your app is using the Support Library for compatibility on versions
as low as Android 2.1, the showAsAction attribute is not available
from the android: namespace. Instead this attribute is provided by the
Support Library and you must define your own XML namespace and use
that namespace as the attribute prefix. (A custom XML namespace should
be based on your app name, but it can be any name you want and is only
accessible within the scope of the file in which you declare it.)
Related Topics
How to Reference an Asset in a Library Project
Android: Setselection Having No Effect on Spinner
Google Maps Fragment Returning Null Inside a Fragment
Android: Programmatically Adding Buttons to a Layout
What Do I Do About: This Requires Android.Permission.Interact_Across_Users_Full
Error: Failed to Resolve: Com.Android.Support:Appcompat-V7:29.0.1'
Restrict Edittext to Single Line
Format Statement in a String Resource File
How to Implement an Android:Background That Doesn't Stretch
Android: How to Get a File's Creation Date
Problem Inflating Custom View for Alertdialog in Dialogfragment
How to Set Text to View from Drawer Header Layout in Navigation Drawer Without Inflating View
Image Share Intent Works for Gmail But Crashes Fb and Twitter
Android: Gles20: Called Unimplemented Opengl Es API
Should One Create a Bond with a Bluetooth Le Device
Intent Does Not Set the Camera Parameters