Vincente Aggrippino
Lurker
I'm going through the tutorial at developer.android.com and I just started the section on adding ActionBar items. I can't get items to actually show in the action bar. They only show in the overflow menu. I'm sure I'm just doing something wrong. I'm probably missing some very simple, but important, detail.
Can anyone help me identify the problem?
___
Update 20150830_194321-0500: Apparently, there's something wrong with my icons. If I remove the line defining the icon, the item appears where it's supposed to. I think that's a subject for another topic. However, there should have been an error message somewhere, shouldn't there? If someone knows what I should be looking for in the logcat (which scrolls by at light-speed in the bottom of Android Studio), I'd welcome any comments
___
___
Update 20150831_222922-0500: The problem was that I was using an icon that was the wrong size. I was using one of the _48dp.png icons and that caused the item not to show at all because it was the wrong size for the action bar. This was pointed out in comments to one of the answers on StackOverflow.
___
I've broken it down to a simple program just to test and find the solution to this one problem. At this point, I'm just trying to add a single action bar item.
I initially thought I wasn't using the appcompatlibrary and I added items to the inflated menu XML file using android:showAsAction="ifRoom". When I did that, the item only shows in the action bar overflow menu.
Thinking there might not be enough room, I even shortened the title to a single letter. That didn't fix it and I also tried the other values "always" and "withText".
Someone on StackOverflow pointed out that I actually am using appcompat because that's how Android Studio generates the code by default, so I changed it to app:showAsAction="ifRoom". When I do this, the item doesn't appear at all. Not even in the overflow.
MainActivity.java
menu_main.xml
Can anyone help me identify the problem?
___
Update 20150830_194321-0500: Apparently, there's something wrong with my icons. If I remove the line defining the icon, the item appears where it's supposed to. I think that's a subject for another topic. However, there should have been an error message somewhere, shouldn't there? If someone knows what I should be looking for in the logcat (which scrolls by at light-speed in the bottom of Android Studio), I'd welcome any comments

___
___
Update 20150831_222922-0500: The problem was that I was using an icon that was the wrong size. I was using one of the _48dp.png icons and that caused the item not to show at all because it was the wrong size for the action bar. This was pointed out in comments to one of the answers on StackOverflow.
___
I've broken it down to a simple program just to test and find the solution to this one problem. At this point, I'm just trying to add a single action bar item.
I initially thought I wasn't using the appcompatlibrary and I added items to the inflated menu XML file using android:showAsAction="ifRoom". When I did that, the item only shows in the action bar overflow menu.
Thinking there might not be enough room, I even shortened the title to a single letter. That didn't fix it and I also tried the other values "always" and "withText".
Someone on StackOverflow pointed out that I actually am using appcompat because that's how Android Studio generates the code by default, so I changed it to app:showAsAction="ifRoom". When I do this, the item doesn't appear at all. Not even in the overflow.
MainActivity.java
Java:
package com.ghodmode.menutest2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@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.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_main.xml
XML:
<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=".MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
android:showAsAction="never" />
<!-- Search, should appear as action button -->
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search_black_48dp"
android:title="@string/action_search"
android:showAsAction="always" />
</menu>
Last edited: