Reputation: 2814
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?
Upvotes: 1
Views: 759
Reputation: 91
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"/>
Upvotes: 3