I tried to add buttons with icons to the Action Bar so I followed the info on the android developer page. However despite having assigned icons, the buttons will only be displayed in the overflow and not on the bar itself. I want the buttons to appear on the bar with their icons. My code:
The onOptionsItemSelected method of the activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_new:
launchAddRuleActivity();
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The onCreateOptionsMenu of 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.main, menu);
return true;
}
The main.xml for the action bar layout:
<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.mhmt.autotextmate.Main" >
<!-- New, should appear as action button -->
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
android:showAsAction="ifRoom"
android:title="#string/action_new"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_save"
android:showAsAction="never"
android:title="#string/action_settings"/>
Lastly, the target and min SDKs:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
I got the drawables from here so the size shouldnt be a problem. This class does extend ActionBarActivity, and imports android.support.v7.app.ActionBarActivity. I'm currently testing on a Google Nexus emulator running Android 5.1.1 with 720x1280 resolution.
I must be doing something wrong, can anyone see what I'm missing to display the buttons with their icons, and not in the overflow?
Try this app:showAsAction="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="com.mhmt.autotextmate.Main" >
<!-- New, should appear as action button -->
<item
android:id="#+id/action_new"
android:icon="#drawable/ic_action_new"
app:showAsAction="always"
android:title="#string/action_new"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_save"
app:showAsAction="always"
android:title="#string/action_settings"/>
I'm following the android developers guide. I have this menu I'm inflating in the action bar:
res/menu/main_activity_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!--Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="always" />
<!-- Settings, should always be in overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never"/>
</menu>
I have a corresponding icon in res/drawable, and am inflating like so:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return true;
}
The problem I'm facing is that the search action gets pushed into the overflow, and I can't figure out how to make it an action button that always shows up in the action bar.
I did not find a duplicate question on this, though there are many related questions.
I'm using this guide http://developer.android.com/guide/topics/ui/actionbar.html to create and add items to the action bar in my app. However the items aren't showing up. This is the menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_alert"
android:icon="#drawable/ic_dialog_alert"
android:title="#string/action_alert"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
I already added the theme in the manifest
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
And finally this is the onCreateOptionsMenu in the Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
I know the Action Bar is working fine because I can hide it with this without problems
ActionBar actionBar = getActionBar();
actionBar.hide();
What am I missing?
When working with devices a hardware settings button your overflow button is moved to the settings button, there are two alternatives to get around this:
xml:
Instantiate your own overflow:
<item android:title=""
android:id="#+id/more"
android:showAsAction="always"
android:icon="#drawable/overflow">
<menu>
<item android:id="#+id/action_alert"
android:icon="#drawable/ic_dialog_alert"
android:title="#string/action_alert"
android:showAsAction="never" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
</item>
.java:
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
If your concern is that there are no icons showing up, try changind android:showAsAction="..." to android:showAsAction="always". This will explicitly show the icons. This is not a good solution if you're supporting multiple screen resolutions though.
XML Namespace
Try a custom namespace like below in your menu.xml file. You can replace custom by whatever you like.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto" >
And then use that namespace in your items (same xml file) instead of the android namespace
<item android:id="#+id/action_alert"
android:icon="#drawable/ic_dialog_alert"
android:title="#string/action_alert"
custom:showAsAction="ifRoom" />
Complete (working) Example
A more complete example can be found at this menu-drawer-compat-example on github. Content of the menu xml file there is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:hcpl="http://schemas.android.com/apk/res-auto" >
<!-- used custom ns hcpl to force display of action buttons -->
<item
android:id="#+id/action_websearch"
android:icon="#drawable/action_search"
android:showAsAction="ifRoom|withText"
android:title="#string/action_websearch"
hcpl:showAsAction="ifRoom"/>
</menu>
This is an example project for the Android compat Actionbar, keep that in mind if you compare your code.
I am trying to add the Search button to the Android Action bar by following this tutorial:
https://developer.android.com/training/basics/actionbar/adding-buttons.html
I modified by minSdkVersion to 11 in my Manifest.
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
I have added the action_search item in a main.xml file located in the menu folder:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/action_search"
android:title="#string/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView"/>
</menu>
Eclipse has provided the method already for the menu, which I have not changed:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
I've added the string to my strings.xml: <string name="action_search">Search</string> and included ic_action_search.png in my drawable folders.
There are no errors upon launch. I see the Settings menu item in the overflow. But I do not see the search button anywhere.
You should remove this value: collapseActionView
Replace this line:
android:showAsAction="always|collapseActionView"
with for example this
android:showAsAction="never"
I have an action bar in my app with 3 items.
Only 2 can be displayed due to space issues, so I'd expect the first to be displayed and the rest to be displayed in the overflow.
However in practice only the first 2 items are shown and there is no overflow detectable.
Here is the relevant code:
list_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_insert"
android:icon="#android:drawable/ic_menu_add"
android:title="#string/menu_insert"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_call"
android:icon="#android:drawable/ic_menu_call"
android:title="#string/menu_call"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_agenda"
android:icon="#android:drawable/ic_menu_agenda"
android:title="#string/menu_agenda"
android:showAsAction="ifRoom|withText"/>
</menu>
Activity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
}
If you want to show the three dots, irrespective of device menu button! then you can call this method in your application class' onCreate method-
private void makeActionOverflowMenuShown() {
//devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
}
}
Output
res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search / will display always -->
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:title="#string/action_search"/>
<!-- Location Found -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:showAsAction="always"
android:title="#string/action_location_found"/>
<!-- More -->
<item
android:id="#+id/a_More"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always"
android:title="More">
<menu>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_refresh"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:showAsAction="never"
android:title="#string/action_help"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_check_updates"/>
</menu>
</item>
</menu>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
}
Download the Action Bar Icon Set
I realize this is not an overflow menu, but it is similar.
Okay, this is simple but was hard to figure out.
You first need a menu item you want to use as the overflow inflater. Example
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
</item>
Once you have your item, add a sub-menu containing your items you want in the overflow menu. Example:
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/aM_Home"
android:icon="#drawable/home"
android:title="Home"/>
</menu>
</item>
On click this will inflate other items within. My application is using ActionBarSherlock 4.0 so before this will work for you, you will need to access the "SplitActionBar". (Will still work on default android Actionbar)
Here's how:
In your AndroidManifest.xml file, you need to add this code under the activity you need the overflow menu in.
android:uiOptions="splitActionBarWhenNarrow"
NOTE: Your item that inflates your overflow menu MUST showAsAction="always"
Vwola! you have an overflow menu! Hope I helped you out. :)
On devices with hardware menu buttons (Galaxy S3, stubborn as Samsung is...) the overflow menu behaves as the 'traditional' menu, by using the hardware menu button.
When you say "overflow" menu, do you mean the three dots that show up in the end to indicate that there are more items.... or do you mean the split actionbar that shows up in the bottom for overflow items?
If you mean the split action bar, you should add this to your activity's manifest file
android:uiOptions="splitActionBarWhenNarrow"
By default, the three dots overflow menu should happen automatically, so it's hard to tell what the problem is from the information you provided above.
To alway show action overflow (three dot) on actionbarcompat:
in menu file, example:
main.xml
<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:title="#string/action_settings"
app:showAsAction="never"/>
<item
android:id="#+id/action_about"
android:title="#string/action_about"
app:showAsAction="never"/>
</menu>
and in activity file:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
It's worked fine for me.
Tested on Google Nexus S, Samsung S3.
It appears that on devices with a menu button the overflow menu does not show in the ActionBar. Also we have no way to know if a device has a hardware menu button before API level 14 (4.0+).
This solution adds a overflow button to a android.support.v7.app.ActionBar that opens the standard options menu on pre-Honeycomb (<3.0) only, otherwise leaving it to the system. In any case you can choose between always or ifRoom for all actions.
Get the overflow icon from the Action Bar Icon Pack.
Add a menu (I'omitting some non-relevant parts as titles, ordering etc.)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_overflow"
android:orderInCategory="100"
android:icon="#drawable/ic_action_overflow"
app:showAsAction="always" />
<item
android:id="#+id/action_first"
app:showAsAction="always" />
<item
android:id="#+id/action_second"
app:showAsAction="ifRoom" />
</menu>
Remove our overflow action on 3.0+
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
menu.removeItem(R.id.action_overflow);
}
return super.onPrepareOptionsMenu(menu);
}
Finally call Activity.openOptionsMenu() from the overflow action.
Er... no. You cannot not call openOptionsMenu() from a menu button... but if you are using AndroidAnnotations you're done easily:
#OptionsItem
void action_overflow() {
openOptionsMenuDeferred();
}
#UiThread
void openOptionsMenuDeferred() {
openOptionsMenu();
}
If not you should do something like this, I suppose
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == id.action_overflow) {
openOptionsMenuDeferred();
return true;
}
}
private Handler handler = new Handler(Looper.getMainLooper());
public void openOptionsMenuDeferred() {
handler.post(new Runnable() {
#Override
public void run() {
openOptionsMenu();
}
}
);
}
Try changing the theme of the app from
Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.Light
Put xmlns:your_app_name="http://schemas.android.com/apk/res-auto" inside menu tag
and instead android:showAsAction="always" use your_app_name:showAsAction="always"
for eg
<item
android:id="#+id/action_search"
android:icon="#drawable/icon_search"
android:title="#string/search"
your_app_name:showAsAction="ifRoom"/>