Android menu item not showing?

I created one menu, but the icon does not appear when it appears, only the text. I am missing settings.

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

+4
source share
2 answers

Check the API level, possibly related to this:

Taken from: Android menu icons are not displayed when API level is above 10

Starting with API level 11 (Android Honeycomb), Android has introduced a new concept for the menu. Devices created for this API level no longer have a menu key. Instead of displaying the menu after pressing a key, a new user interface component appears: the action bar. The action bar now displays as many menu items as space allows, and after that a button is created that shows the rest of the menu items in the overlay.

I would suggest that you use some theme for your activity that prevents the Actionbar from appearing and therefore the menu items are not visible. Also, read the tablet and phone support guide to understand how the new action bar works.

+2
source

You must add showAsAction = "ifRoom" so that it appears as an icon if there is enough space.

Also note that you should check the namespace of this attribute if you are using any 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> 
0
source

Source: https://habr.com/ru/post/1400132/


All Articles