0

I am following the tutorial of the following site in adding button to action bar http://developer.android.com/training/basics/actionbar/adding-buttons.html#AddActions

Following the tutorial, I have created a new XML file, \res\menu\main_activity_actions.xml with the following content

<?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:orderInCategory="100"
android:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item   android:id="@id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>

And my MainActivity.java, I have modified the onCreateOptionsMenus() as follow

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

However, after compilation, I can only the search and setting button after pressing menu button, not showing in the actiionbar by deafult. Any hints?

Thanks

JC

J.C.
  • 5
  • 1

4 Answers4

0

if you are targeting from version 2.2, you have to create a custom namespace and add in xml or if you are targeting from 3.0 versions use the same namespace

As for the above question, try

android:showAsAction = "ifRoom"

or use custom namespace, after

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myApp="http://schemas.android.com/apk/res-auto"

and

myApp:showAsAction:"ifRoom"
Vishnu Prabhu
  • 449
  • 1
  • 5
  • 19
0

You need to take a look at the documentation, as it's all there: http://developer.android.com/guide/topics/ui/actionbar.html

However, just so you have an answer on here, as does anyone else who comes to SO for the answer, it's a very easy fix. In your menu XML file, you just need to include the option myApp:showAsAction="always" in the <item> tag, which will force it to always appear. You can also specify ="never" which will make it only ever appear when you press the menu button, or ="ifRoom", the use of which should be fairly obvious

Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
0

The overflow menu will only appear in the actionbar if the phone does not have a menu button as explained here.

If you really want this in the actionbar at all times then you could use this.

You could also do android:showAsAction:"ifRoom". If there is no more room then it will be automatically put in an overflow menu.

You could also create your own menu (not attached to the menu button). You can find out how to do this here.

Community
  • 1
  • 1
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
0

Try to add:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

and change

android:showAsAction="always"

to

yourapp:showAsAction="ifRoom"

final version would be

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">

<!-- search, should appear as action button -->
<item   android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:orderInCategory="100"
yourapp:showAsAction="always" />
<!-- Settings, should always be in the overflow -->
<item   android:id="@id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>

If you are following the instruction, dont forget to use AppCompat Theme (change it in your AndroidManifest.xml )