Shane Gannon
Shane Gannon

Reputation: 7728

Android Action Button Icons are missing

I'm new to Android & attempting to follow the tutorials. The initial tutorial was going well but then I hit this one

https://developer.android.com/training/basics/actionbar/adding-buttons.html

where I'm finding I can't get the action buttons to display an icon. Even if I set

 android:showAsAction="always"

In an attempt to workaround this issue I started another similar tutorial

http://www.androidhive.info/2013/11/android-working-with-action-bar/

but only ended up hitting the same issue.

I'm using API 11 & not going near the support bundle. I can get the text for the menu items to appear in the drop down. Just can't seem to get icons.

I tried both sets of icons from the Action Bar Icon Set. My res folder has the following drawable structure

- drawable
- drawable-hdpi
- drawable-mdpi
- drawable-xhdpi
- drawable-xxhdpi

With nothing in drawable & the appropriate icons in the other folders.

This seems like such a basic step that I'm surprised I'm stuck.

Any suggestions?

Edit: Adding code as suggested

MainActivity.java

package com.example.gannons.actionbar;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

@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.activity_main_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

@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);
}
}

activity_main_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Search / will display always -->
<item android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"
    android:showAsAction="always"
/>

<!-- Location Found -->
<item android:id="@+id/action_location_found"
    android:icon="@drawable/ic_action_location_found"
    android:title="@string/action_location_found"
    android:showAsAction="always"
/>

Also I'm using Android Studio in case that makes a difference.

Upvotes: 2

Views: 1608

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 199825

Because your Activity extends ActionBarActivity, you are in fact, using the support library (as is recommended if you want something that looks consistent from API11+ - remember the default looks very different on API21+). You can switch your activity to extend Activity and you'll use the framework version of the action bar which will work with your activity_main_actions.xml as you have written.

Otherwise, change your android:showAsAction to app:showAsAction and it will look and act consistently across all API versions.

Upvotes: 1

Related Questions