• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Icons won't show in ActionBar

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
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:
@Vincente Aggrippino

I highly recommend switching to the new Toolbar that replaces the old ActionBar. The Toolbar gives you much more control and customization. You can even place it anywhere on the screen you want like the bottom for example.

In case you're interested I'll give you a quick guide on how I use it in my apps...

First make sure your "build.gradle" has the latest appcompat dependency:
Code:
compile 'com.android.support:appcompat-v7:23.0.0'

Your activity should extend "AppCompatActivity" instead of "ActionBarActivity" Like this (which you already have):
Code:
public class MainActivity extends AppCompatActivity {

At the top of all my classes that will use a toolbar:
Code:
import android.support.v7.widget.Toolbar;

Inside the onCreate of all my classes that will use a toolbar:
"R.id.mytoolbar" is referrencing the toolbar id in the toolbar layout xml (more on that later).
Code:
Toolbar toolbar = (Toolbar) findViewById(R.id.mytoolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Now setup the toolbar layout xml, I name mine "toolbar.xml":
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mytoolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>

After the "toolbar.xml" is setup you can use it in any activity xml by including it above all objects in your activity xml like this:
Code:
<include layout="@layout/toolbar" />

To place objects underneath the toolbar in any activity xml include this in your object (remember "mytoolbar" is the toolbar id used in "toolbar.xml"):
Code:
android:layout_below="@+id/mytoolbar"
tools:ignore="UnknownIdInLayout"

You can totally customize the "toolbar.xml" to your liking. You can add images, change it's height, change it's screen location, change color, and just about anything you want, just be creative.

As for setting up the overflow menu that can be a lengthy tutorial so I'll have to cover that another day with more time.

Here's a screenshot of what the toolbar looks like in one of my apps with an overflow menu that includes share, about, and other apps :
7660018.png
 
Back
Top Bottom