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>
The Android overflow menu is accessed from the far right of the actions toolbar at the top of the display of the running app. This menu provides a location for applications to provide additional options to the user.
The right-hand side of the action bar shows the actions. The action buttons (3) show the most important actions of your app. Actions that do not fit in the action bar are moved to the action overflow, and an overflow icon appears on the right. Tap on the overflow icon to display the list of remaining action views.
The action overflow in the action bar provides access to your app's less frequently used actions. The overflow icon only appears on phones that have no menu hardware keys. Phones with menu keys display the action overflow when the user presses the key. Action overflow is pinned to the right side.
Show back button using actionBar. setDisplayHomeAsUpEnabled(true) this will enable the back button. Custom the back event at onOptionsItemSelected. This will enable the back function to the button on the press.
General reference
1 - Override onCreateOption and inflate the menu
Note
Doesn't matter if you return true or call super
@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 super.onCreateOptionsMenu(menu);
}
2 - Add the namespace to the menu.xml file.
xmlns:**yourapp**="http://schemas.android.com/apk/res-auto"
3 - set the showAsAction to always or ifRoom
**yourapp**:showAsAction="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]
main.xml
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_action_settings"
android:title="@string/action_settings"
yourapp:showAsAction="ifRoom" />
on your 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 super.onCreateOptionsMenu(menu);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With