icon in menu not showing in android - android

I want to add menu handler to my project. I read http://developer.android.com/guide/topics/ui/menus.html too, its very simple but the icon is not shown. I am very confused. I even added a menu item programmatically.
My code is:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Quit").setIcon(R.drawable.ic_launcher);
getMenuInflater().inflate(R.layout.menu, menu);
return true;
}
and in xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_bookmark"
android:icon="#drawable/update"
android:title="#string/Update" />
</menu>

After Long try i found below solution which might help others to save there time. Basically, the solution provided by "lbarbosa", i like to thanks to him sincerely.
Tried this based on the previous answers and it works fine, at least with more recent versions of the support library (25.1):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
if(menu instanceof MenuBuilder){
MenuBuilder m = (MenuBuilder) menu;
m.setOptionalIconsVisible(true);
}
return true;
}

If you're running your code on Android 3.0+, the icons in the menu are not shown by design. This is a design decision by Google.
You can read more about it in this on Android developers blog.

No matter what design choices where made by the system, you can circumvent this with the solution provided in the top upvoted answer to this question
Code below for completeness. Tested working on android.support.v7.app.ActionBarActivity
#Override
public boolean onMenuOpened(int featureId, Menu menu)
{
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e(TAG, "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}

Old question but hope it will help someone.
use the following code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_item_share"
android:title="Share"
app:showAsAction="always"
android:icon="#drawable/share" /></menu>
note i used app:showAsAction instead of android:showAsAction

You can add to your XML file the attribute android:showAsAction="always" inside your item element. It then will show the relevant menu option as an icon inside your action bar.
Note that it will be instead of the text in the menu.
For further read, look here under android:showAsAction.

/* menu code */
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/one" android:title="one"
android:icon="#mipmap/ic_launcher" app:showAsAction="always" />
<item android:id="#+id/two" android:title="two"
android:icon="#mipmap/ic_launcher" app:showAsAction="always" />
<item android:id="#+id/three" android:title="three"
android:icon="#mipmap/ic_launcher" />
</menu>
/* Java code */
#SuppressLint("RestrictedApi")
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu,menu);
if(menu instanceof MenuBuilder){
MenuBuilder m = (MenuBuilder) menu;
m.setOptionalIconsVisible(true);
}
return true;
}

Forget all those, do this step.
add app:showsAsAction="always" to your item. If you use android:showsAsAction="always" you won't get the solution.
Try with adding app attribute to your item.

I put explicit icons in onCreateOptionsMenu method using below code
for (int i = 0; i < menu.size(); i++) {
MenuItem item = menu.getItem(i);
if (item.getItemId() == R.id.print) {
item.setIcon(getDrawable(R.drawable.print));
}
}

Working with android.support.v7.app.AppCompatActivity, is making the input for the icons a very difficult task. You need to implement onMenuOpen, super method from AppComactActivity class. After this check, if the menu is not null. If the menu is not null pass Method class like this and setOptionalIconsVisible to true with boolean.
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e("MAIN", "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}

Just add this attr inside item tag in your menu.xml
app:showAsAction="always

#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "androidDemo").setIcon(R.drawable.ic_launcher);
getMenuInflater().inflate(R.layout.menu, menu);
return true;
}
and in xml:
<item android:id="#+id/menuUpdate"
android:icon="#drawable/update_icon"
android:title="#string/Update"
android:showAsAction="always"/>

Related

Android: Can't change properties of MenuItem in OnCreateOptionsMenu

It's very simple. I have following code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
if(isReturn) {
inflater.inflate(R.menu.menu_returned, menu);
return super.onCreateOptionsMenu(menu);
} else {
inflater.inflate(R.menu.menu_return, menu);
MenuItem red = menu.findItem(R.id.action_error);
red.setIcon(R.mipmap.ic_error_red);
// if (sellStatus.equals(Bill.ERROR_CREATE)) {
// red.setIcon(R.mipmap.ic_error_red);
// } else if (sellStatus.equals(Bill.ERROR_DATABASE)) {
// red.setIcon(R.mipmap.ic_error_orange);
// } else{
// red.setVisible(false);
// }
return true;
}
}
XML menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_return"
android:title="Return"
app:showAsAction="withText|always"/>
<item
android:id="#+id/action_error"
android:title=""
app:showAsAction="always"/>
</menu>
I'm trying to change MenuItem's properties(setIcon, setVisible). But it don't change MenutItem properties(I guess , It can't connect MenuItem).
But it's taking onOptionsItemSelected and icons from xml.
Is it android bug or am i missing some pattern?
Change your menu appearance in the onPrepareOptionsMenu instead of in the onCreateOptionsMenu. This allow you to change the menu appearance azccording to the state of your app.

Animate Between Visibility Modes For Menu Item

I am trying to animate between the visibility mode for a menu.
By default all menu items are hidden but when the user clicks on the edit button i want to show all the items with an animation.
I have achieved the first part of changing the visibility of the menu items and that works fine but the animation part crashes the app.
Here is my code.
When user clicks on edit this is called.By default edit_mode is false.
if (!edit_mode) {
edit_mode = true;
supportInvalidateOptionsMenu();
}
This is the menu code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_add__custom, menu);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem photo = menu.findItem(R.id.photo);
photo.setVisible(edit_mode);
if (edit_mode)
photo.getActionView().animate().alpha(1.0f);
MenuItem date = menu.findItem(R.id.date);
date.setVisible(edit_mode);
if (edit_mode)
date.getActionView().animate().alpha(1.0f);
MenuItem done = menu.findItem(R.id.done);
done.setVisible(edit_mode);
if (edit_mode)
done.getActionView().animate().alpha(1.0f);
return edit_mode;
}
menu.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">
<item
android:id="#+id/date"
android:icon="#drawable/ic_event_white_24dp"
android:orderInCategory="200"
android:title="Date"
app:showAsAction="ifRoom" />
<item
android:id="#+id/done"
android:icon="#drawable/ic_done_white_24dp"
android:orderInCategory="300"
android:title="Done"
app:showAsAction="ifRoom" />
<item
android:id="#+id/photo"
android:icon="#drawable/ic_photo_white_24dp"
android:orderInCategory="100"
android:title="Done"
app:showAsAction="ifRoom" />
I sure that crash that you are having is there because of NullException thrown by getActionView(). First of all to animate that way you have set the actionView first during onCreateOptionMenu(). That way when you get the actionView in onPrepareOptionsMenu it wont crash because of that and then you can animate it. The onPrepareOptionsMenu executes when you press the menu button so your logic to animate it that time is correct.
If its just the text you want to show in menu item, it should go like this,
final MenuItem photo;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_my_report, menu);
photo = menu.findItem(R.id.action1);
TextView textView = new TextView(this);
textView.setText("I am menu item");
photo.setActionView(textView);
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if(someCondition)
{
photo.getActionView().animate().alpha(1.0f);
}
return super.onCreateOptionsMenu(menu);
}
In case you want to have the a complex and customise text you can set it using the layoutInflator service. This could go in your onCreate,
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView view = (ImageView)inflater.inflate(R.layout.some_view, null);
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.fade);
and onCreateOptionMenu,
view.startAnimation(rotation);
photo.setActionView(view);
Its just to get you an idea what needed to be done, you can play around with this and can suit your need.

Show icons in Overflow Menu in Contextual Action Bar

I know how to show icons in the overflow menu of the ActionBar. This is what I use,
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (NoSuchMethodException e) {
Log.e("TAG", "onMenuOpened", e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
But when I long-press on any item of my ListView the CAB starts. Now, when I open the overflow menu of the CAB, the menu doesn't have any icons. How can I do that?
Thanks in advance.
I waited for 2 days but didn't get any answer on this. So solved it myself.
The idea is quite simple here. You need to create your own overflow item in the and the create a nested menu to show both the icon and the text.
See the example code below,
<item
android:id="#+id/overflow"
android:icon="#drawable/ic_overflow_white"
android:orderInCategory="201"
android:title="#string/overflow"
app:showAsAction="always">
<menu>
<item
android:id="#+id/cab_menu_select_all"
android:icon="#drawable/ic_select_all_grey"
android:orderInCategory="100"
android:title="#string/cab_menu_select_all"
app:showAsAction="always|withText"></item>
</menu>
</item>
The trick here is to create nested menus. You can add as many items as you want.

Android: AppCompat v21 Menu item problems

I'm busy with changing my existing app to material design, but i'm having trouble with the menu items inside the ActionBar.
First of all in some activities i'm using a searchview, But i have read that you need to use a custom prefix inside the searchview menu xml. So i did:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search_light"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
But everytime I'm getting the error:
"Should use android:showAsAction when not using the appcompat library"
if i change app:showAsAction="ifRoom|collapseAction" to android:showAsAction="ifRoom|collapseActionView"
I'm not getting a searchview that take the whole lenght of the actionbar, just some strange square in the right corner where the items are placed. Here is my code of the onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchview, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView search = (SearchView) MenuItemCompat.getActionView(searchItem);
if(search != null)
{
search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
try {
new LoadResultsTask().execute(URLEncoder.encode(query, "UTF-8"));
} catch (UnsupportedEncodingException e) {
Toast.makeText(IndexerOverviewActivity.this, "Could not parse Query", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return true;
}
#Override
public boolean onQueryTextChange(String search) {
return true;
}
});
}
search.setIconifiedByDefault(false);
return super.onCreateOptionsMenu(menu);
}
I noticed aswell that all my actionbar menu items that has android:showsAsAction="always" are shown as android:showsAsAction="never" when i start my application on my device. So my first guess is it has everything to do with the prefix to use in the menu xml files.
Pffff, after just a single "clean" it works perfectly now...
It took me freaking 2 hours to fix this.

android Menu icon not showing

I am having trouble getting the menu icon to show up on the action bar. I tried setting
android:showAsAction="always"
but even that doesn't work. In an earlier project I just used
android:showAsAction="ifRoom|withText"
and that worked. The only thing different with this project is that I am building with gradle instead of ANT. Could that make a difference?
Here is my menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/new_transaction"
android:icon="#drawable/ic_menu_add"
android:title="#string/new_transaction"
android:titleCondensed="#string/new_transaction_condensed"
android:showAsAction="always" />
</menu>
I copied each version of ic_menu_add.png from the android library to each respective drawable folder.
And here is my onCreateOptionsMenu in my ActionBarActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.account_pager, menu);
return true;
}
No matter what I try, the icon does not show up. I've tried changing the max and min API level, but to no avail. In order to click the menu item, I have to go to the overflow menu and then click the dropdown menu item there, which is definitely less than ideal. How can I make the icon show?
My min API level is 11, and my max is 20. I am developing on an HTC One running Android 4.1.2, if any of that matters.
Thanks in advance.
Try this:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (NoSuchMethodException e) {
Log.e("", "onMenuOpened", e);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}

Categories

Resources