My android app supports 2.2 and higher. I'm using the appcompat support library for the action bar, so it should only show if there are things that don't fit. I want my action bar to support the overflow button (the three vertical squares) that reveals a menu with the other items when clicked.
In my menu file, I have three items set up. However on the app I only see two of them, and the overflow button is not showing as well.
activity_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sord.ids_connect="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:icon="@drawable/checkbox"
android:title="@string/action_settings"
sord.ids_connect:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings2"
android:icon="@drawable/checkbox_checked"
android:title="@string/action_settings"
sord.ids_connect:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings3"
android:icon="@drawable/ic_launcher"
android:title="@string/action_settings"
sord.ids_connect:showAsAction="ifRoom" />
</menu>
java file
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class Activity_Menu extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_menu, menu);
//return true;
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
manifest
<activity
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
android:name="sord.ids_connect.Activity_Menu"
android:screenOrientation="portrait"
android:label="@string/title_activity_menu" >
</activity>
General reference
1 - Override onCreateOption and inflate the menu
Note
Doesn't matter if you return true or call super
2 - Add the namespace to the menu.xml file.
3 - set the showAsAction to always or ifRoom
4 - if you are using appcompat make sure that you Activity extends ActionBarActivity , you don't have to change any value of the ActionBar to be able to see you menuOption in the bar.
Finally you will have something like [remember to user a proper namespace for yourapp]
on your Activity
If you are using the support library you'll need to declare the showAsAction from your own namespace; which will look like this
app:showAsAction
. You can refer to eOnOe's answer for this.Using
android:showAsAction
won't work with the support library because it uses it's own implementation of the action bar rather than the framework's implementation of it.You can also always group items into the icon for example:
Android show overflow menu only if your device doesn't have menu button. Of course you can create hack a do it default for everyone device but it is't recommended solution. And for your every items add
android:showAsAction="never"
and this say your device show overflow menu if you don't have menu button.That is the overflow.
Because the other two are in the action bar. You specified
ifRoom
, and there were room for two, not three.You need to menu the menu layout as below