nivve16
nivve16

Reputation: 41

android Menu Item not showing icon?

I have create a single item menu, but the icon does not appear when it pops up, only the text does. Am I missing a setting.

java File

package com.menu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;

public class MymenuActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
}

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/icon"
        android:icon="@drawable/ic_launcher" />
    <item android:id="@+id/text"
        android:title="Text" />
    <item android:id="@+id/icontext"
        android:title="Icon"
        android:icon="@drawable/ic_launcher" />

</menu>

please help

Upvotes: 4

Views: 6665

Answers (2)

android developer
android developer

Reputation: 116322

You should add showAsAction="ifRoom" , so that it will be shown as an icon if there is enough room.

Also, note that you should check the namespace of this attribute in case you use any kind of support library.

Example:

<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.example.test.MainActivity" >

    <item
        android:icon="@drawable/ic_action_alerts_and_states_warning_holo_dark"
        app:showAsAction="ifRoom" 
        android:title="sorting"/>
</menu>

Upvotes: 0

user1723341
user1723341

Reputation: 1107

Check you API level, could possibly be related to this:

Taken From: Android menu icons are not displaying when the API level is above 10

Starting with API Level 11 (Android Honeycomb) Android introduced a new concept for menus. Devices build for that API Level do not have a menu key anymore. Instead of showing a menu after a key is pressed there is a new UI Component: the Actionbar. The Actionbar now shows as much menu items as the space allows and after that creates a button that will show the rest of the menu items in an overlay.

I would assume that you are using some kind of theme for your activity that prevents the Actionbar from appearing and therefore no menu items are visible. Also read the guide on how to support Tablets and Handsets to begin to understand how the new actionbar works.

Upvotes: 2

Related Questions