How to Show Icon with Menus in android - android

I want to show icon in Option Menus. I tried many methods but I can't find success.In my toolbar some icon already put but i want icon in my Option menu.
I want same as below image:-
My 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"
tools:context=".MainActivity">
<item
android:id="#+id/action_search"
android:title="#string/action_search"
android:orderInCategory="1"
android:icon="#drawable/search"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_cart"
android:title="#string/action_search"
android:orderInCategory="2"
android:icon="#drawable/shoppingcart"
android:actionLayout="#layout/feed_update_count"
app:showAsAction="ifRoom" />
<item
android:id="#+id/menu_overflow"
android:title="#string/my_cart"
android:icon="#drawable/ic"
android:orderInCategory="11111"
app:showAsAction="always">
<menu>
<item
android:id="#+id/login"
android:title="#string/login"
android:orderInCategory="1"
android:icon="#drawable/account"
app:showAsAction="always|withText"/>
<item
android:id="#+id/my_order"
android:title="#string/my_order"
android:orderInCategory="2"
android:icon="#drawable/track"
app:showAsAction="withText"/>
<item
android:id="#+id/wishlist"
android:title="#string/wishlist"
android:orderInCategory="3"
android:icon="#drawable/wishlist"
app:showAsAction="withText"/>
</menu>
</item>
</menu>
I am new in android developing anyone can help me how to show icon in option menu. Thanks in advance!

By default the overflow menu will disable the icon and only show text, you need some workaround.
1st way
You can enable it by adding the following codes into your class
#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);
}
2nd way
Create your own overflow item and a nested menu to show both the icon and the text.
Example,
<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>

try this code its works for you
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always"
android:title="#string/action_search"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_location_found"
android:icon="#drawable/ic_action_location_found"
android:showAsAction="always"
android:title="#string/action_location_found"/>
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/ic_action_menu"
android:orderInCategory="11111"
android:showAsAction="always">
<menu>
<!-- Refresh -->
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="ifRoom"
android:title="#string/action_refresh"/>
<!-- Help -->
<item
android:id="#+id/action_help"
android:icon="#drawable/ic_action_help"
android:showAsAction="never"
android:title="#string/action_help"/>
<!-- Check updates -->
<item
android:id="#+id/action_check_updates"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="never"
android:title="#string/action_check_updates"/>
</menu>
</item>
</menu>
- See more at: http://www.theappguruz.com/blog/android-working-android-actionbar#sthash.ArNQTEBF.dpuf

Create your menu xml as follow
<item
android:id="#+id/menu_item_action_parameters"
android:title="#string/text_parameters"
android:icon="#drawable/ic_menu_parameter"
app:showAsAction="ifRoom|withText"/> >
<menu>
<item
android:id="#+id/action_dropdown1"
android:title="#string/dropdown_1" />
<item
android:id="#+id/action_dropdown2"
android:title="#string/dropdown2" />
<item
android:id="#+id/action_dropdown3"
android:title="#string/dropdown3" />
</menu>
</item>
<item
more item
</item>
Then
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_dropdown1:
.....
return true;
case R.id.action_dropdown2:
.....
return true;
.....
default:
return super.onOptionsItemSelected(item);
}
}
this is completely working code.Mark this answer up if it helps. :) Happy coding.

you Need to add tools:context="your class" to menu tag
<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=".activities.BaseActivity">
<item
android:id="#+id/action_notification1"
android:icon="#drawable/one"
android:title="action_notification"
app:showAsAction="always"/>
<item
android:id="#+id/action_notification2"
android:icon="#drawable/two"
android:title="action_notification"
app:showAsAction="always"/>
<item
android:id="#+id/action_notification3"
android:icon="#drawable/three"
android:title="action_notification"
app:showAsAction="always"/>
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
you shold write tool:context to menu tag then run you will get icons to your text

Related

Toolbar icons disappearing after expanding the SearchView in Android

here's my problem:
I have that nice toolbar with the icons in landscape mode:
after expanding the search view and showing the popup menu the "add" item appears (I thought that it shouldn't):
then returning with the back arrow key, as you see, the add button goes:
and you won't find it in the popup menu anymore:
I'm using support:appcompat-v7:25.1.0, and here's my menu code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/app_bar_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:showAsAction="always|collapseActionView"
android:enabled="true"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="ifRoom"
android:id="#+id/add" />
<item android:title="Settings"
android:id="#+id/settings"
app:showAsAction="never"
android:icon="#drawable/ic_action_settings"
android:enabled="true"
android:visible="true" />
<item android:title="Feedback"
android:id="#+id/feedbvack"
app:showAsAction="never"
android:icon="#drawable/ic_action_feedback"
android:enabled="true"
android:visible="true" />
</menu>
I can set the add button showAsAction to "always" but I know that this is discouraged.
Does anyone here know why is there this behavior? and how can I prevent that?
Thanks in advance.
You can try to use this:
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
//or use invalidateOptionsMenu();
return true;
}
});
So when SearchView will be collapsed Toolbar will reallocate items and ifRoom items will be visible. I had this bug too and solved it this way.
Try this
<item
android:id="#+id/app_bar_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:showAsAction="always|collapseActionView"
android:enabled="true"
android:visible="true"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="always"
android:id="#+id/add" />
<item android:title="Settings"
android:id="#+id/settings"
app:showAsAction="never"
android:icon="#drawable/ic_action_settings"
android:enabled="true"
android:visible="true" />
<item android:title="Feedback"
android:id="#+id/feedbvack"
app:showAsAction="never"
android:icon="#drawable/ic_action_feedback"
android:enabled="true"
android:visible="true" />
Set app:showAsAction to always to make sure it will be visible always.
Use of ifRoom has this feature if space available it will show or else it will hide it better use always or never
<item android:title="Add"
android:enabled="true"
android:icon="#drawable/ic_action_add"
android:visible="true"
app:showAsAction="always"
android:id="#+id/add" />
#Kovy has answered above with the fix for this bug and I confirm it fixes the bug. Thank you very much, mate! However, the above function has been deprecated in favour of individual menu items OnActionExpandListener. Example of it is as posted below:
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
supportInvalidateOptionsMenu();
return true;
}
});

Unable to show the 3dot menu

I want to be able to show the free-dot menu always without using ActionBar. Just 3 dots menu + items in it, that's it.
How can I do that? Here's my code
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyMainActivity">
<item
android:id="#+id/empty"
android:showAsAction="always"
android:title="#string/dot_menu"
android:icon="#android:drawable/ic_menu_more">
<menu>
<item
android:id="#+id/item1"
android:showAsAction="ifRoom"
android:title="#string/item1" />
<item
android:id="#+id/item2"
android:showAsAction="ifRoom"
android:title="#string/item2" />
<!-- ........... -->
And:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//.....
}
It doesn't show a menu at all.
Try this code, I hope it's helpful.
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/item_admin"
android:showAsAction="ifRoom|withText"
android:title="#string/item_admin"
android:visible="false"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_masterpass_preferences"
android:showAsAction="ifRoom|withText"
android:title="#string/text_1"
android:visible="true"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_shipping_addresses"
android:showAsAction="ifRoom|withText"
android:title="#string/text_2"
android:visible="true"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_switch_language"
android:showAsAction="ifRoom|withText"
android:title="#string/text_3"
android:visible="false"
tools:ignore="AppCompatResource" />
<item
android:id="#+id/item_support_contact"
android:showAsAction="ifRoom|withText"
android:title="#string/text_4"
android:visible="true"
tools:ignore="AppCompatResource" />

Add actionbar toggle button next to the setting menu in the actionbar

I have map activity and I am trying to add toggle button to the actionbar next to the setting menu which contains the map type but I am facing a problem: the toggle button is being added to the setting items menu and not directly to the actionbar.
How can I add this toggle button to the actionbar next to the setting menu?
map_menu.xml
<item
android:id="#+id/mapTypeNormal"
android:showAsAction="always"
android:title="Normal"/>
<item
android:id="#+id/mapTypeSatellite"
android:showAsAction="always"
android:title="Satellitte"/>
<item
android:id="#+id/mapTypeTerrain"
android:showAsAction="always"
android:title="Terrain"/>
<item
android:id="#+id/mapTypeHybrid"
android:showAsAction="always"
android:title="Hybrid"/>
<item
android:id="#+id/mapTypeNone"
android:showAsAction="always"
android:title="None"/>
<item android:id="#+id/menu_toggle"
android:showAsAction="ifRoom|withText"
android:icon="#drawable/off"
android:title="Share"
/>
map activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map_menu, menu);
System.out.println("ABC MAP onCreateOptionsMenu was invoked.");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapTypeNone:
map.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
case R.id.mapTypeNormal:
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.mapTypeSatellite:
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.mapTypeTerrain:
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.mapTypeHybrid:
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
default:
break;
}
switch (item.getItemId()) {
case R.id.menu_toggle:
if (birthSort) {
// change your view and sort it by Alphabet
item.setIcon(R.drawable.on);
item.setTitle("On");
birthSort = false;
} else {
// change your view and sort it by Date of Birth
item.setIcon(R.drawable.off);
item.setTitle("Off");
birthSort = true;
}
}
return super.onOptionsItemSelected(item);
}
}
Edit:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/mapTypeNormal"
android:showAsAction="never"
android:title="Normal"/>
<item
android:id="#+id/mapTypeSatellite"
android:showAsAction="never"
android:title="Satellitte"/>
<item
android:id="#+id/mapTypeTerrain"
android:showAsAction="never"
android:title="Terrain"/>
<item
android:id="#+id/mapTypeHybrid"
android:showAsAction="never"
android:title="Hybrid"/>
<item
android:id="#+id/mapTypeNone"
android:showAsAction="never"
android:title="None"/>
<item
android:id="#+id/menu_toggle"
android:icon="#drawable/off"
android:showAsAction="always"
android:title="Share"/>
</menu>
While using AppCompat library, the showAsAction from http://schemas.android.com/apk/res/android doesn't work for some reason, you'll have to use one that comes from http://schemas.android.com/apk/res-auto.
You'll can add the required schema with the namespace app as shown below:
xmlns:app="http://schemas.android.com/apk/res-auto"
And then for menu items that you want to keep visible on the ActionBar, you can add the following attribute:
app:showAsAction="always"
and for the others you can add:
app:showAsAction="ifRoom"
So your map_menu.xml would be:
<?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/mapTypeNormal"
app:showAsAction="never"
android:title="Normal"/>
<item
android:id="#+id/mapTypeSatellite"
android:showAsAction="never"
android:title="Satellitte"/>
<item
android:id="#+id/mapTypeTerrain"
app:showAsAction="never"
android:title="Terrain"/>
<item
android:id="#+id/mapTypeHybrid"
app:showAsAction="never"
android:title="Hybrid"/>
<item
android:id="#+id/mapTypeNone"
app:showAsAction="never"
android:title="None"/>
<item
android:id="#+id/menu_toggle"
android:icon="#drawable/off"
app:showAsAction="always"
android:title="Share"/>
</menu>
I did this and it's working for me
<item
android:id="#+id/mapTypeNormal"
app:showAsAction="never"
android:title="Normal"/>
<item
android:id="#+id/mapTypeSatellite"
app:showAsAction="never"
android:title="Satellitte"/>
<item
android:id="#+id/mapTypeTerrain"
app:showAsAction="never"
android:title="Terrain"/>
<item
android:id="#+id/mapTypeHybrid"
app:showAsAction="never"
android:title="Hybrid"/>
<item
android:id="#+id/mapTypeNone"
app:showAsAction="never"
android:title="None"/>
<item
android:id="#+id/menu_toggle"
android:icon="#drawable/off"
app:showAsAction="always"
android:title="Share"/>

Display menu items on ActionBar

I want to display my menu items on ActionBar with android
I found some samples but It was not what I wanted
Menu should be similar picture below(Overflow Icon or text)
My menu.xml is :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_refresh"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/icon"
android:visible="true"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings">
<item
android:id="#+id/mapMenu"
android:title="map menu" />
<item
android:id="#+id/favMenu"
android:title="favorite" />
<item
android:id="#+id/listMenu"
android:title="List Menu" />
</item>
and my android code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getApplicationContext(), item.getTitle() + " selected", Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.mapMenu:
// do something
break;
case R.id.favMenu:
// do something
break;
case R.id.listMenu:
// do something
break;
}
return true;
}
Once write following in all item tags
android:showAsAction="never"
then all items will display in overflow.
Ex:
<item
android:id="#+id/mapMenu"
android:showAsAction="never"
android:title="map menu" />
Hope this will helps you.
If you want to show the three dots, irrespective of device menu button, then you can call below code (which is based on reflection) in your application class' onCreate method-
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
Caution: This is an awful hack that breaks consistency with the rest
of the apps on the platform
Thanks all
but I added android:showAsAction="ifRoom|withText" and result is :
I also tested android:showAsAction="ifRoom|withText" and menu items hid
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/main_menu"
android:showAsAction="always"
android:icon="#drawable/icon"
android:visible="true">
<item
android:id="#+id/action_refresh"
android:showAsAction="never"
android:title="Refresh"/>
<item
android:id="#+id/action_settings"
android:title="Settings"
android:showAsAction="never" />
<item
android:id="#+id/mapMenu"
android:title="map menu"
android:showAsAction="never" />
<item
android:id="#+id/favMenu"
android:title="favorite"
android:showAsAction="never" />
<item
android:id="#+id/listMenu"
android:title="List Menu"
android:showAsAction="never" />
</item>
</menu>
android:showAsAction: When and how this item should appear as an action item in the Action Bar. A menu item can appear as an action item only when the activity includes an ActionBar (introduced in API Level 11).
Try the given code. First you create root item keep its android:showAsAction:Always then inside that root item create other items with android:showAsAction:never.

Deeply nested menu items do not fire on click - Android

It may be worth noting that my R.java file disappeared this morning because I used an incorrect menu icon name in this menu XML file. I have since corrected the name and regenerated the R.java file.
What I have found out by debugging:
Any menu item that takes 4 clicks to get to does not fire the onOptionsItemSelected listener in Android.
Can I make these 4-click menuItems fire the onOptionsItemSelected listener? Thanks.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_sticker"
android:showAsAction="always"
android:icon="#drawable/ic_action_add"
android:title="Add Sticker">
<menu>
<item
android:id="#+id/menu_male"
android:showAsAction="always"
android:title="Male">
<menu>
<item
android:id="#+id/menu_add_clothes"
android:showAsAction="always"
android:title="Add Clothes">
<menu>
<item
android:id="#+id/menu_maleHat"
android:showAsAction="always"
android:title="Hat"/>
<item
android:id="#+id/menu_maleTop"
android:showAsAction="always"
android:title="Top"/>
<item
android:id="#+id/menu_malePants"
android:showAsAction="always"
android:title="Pants"/>
</menu>
</item>
<item
android:id="#+id/menuMaleAccessories"
android:showAsAction="always"
android:title="Add Accessories">
</item>
<item
android:id="#+id/menuMaleHair"
android:showAsAction="always"
android:title="Add Hair">
</item>
</menu>
</item>
<item
android:id="#+id/menu_female"
android:showAsAction="always"
android:title="Female">
<menu>
<item
android:id="#+id/menu_add_clothes_female"
android:showAsAction="always"
android:title="Add Clothes">
<menu>
<item
android:id="#+id/menu_femaleHat"
android:showAsAction="always"
android:title="Hat"/>
<item
android:id="#+id/menu_femaleTop"
android:showAsAction="always"
android:title="Top"/>
<item
android:id="#+id/menu_femalePants"
android:showAsAction="always"
android:title="Pants"/>
</menu>
</item>
<item
android:id="#+id/menuFemaleAccessories"
android:showAsAction="always"
android:title="Add Accessories">
</item>
<item
android:id="#+id/menuFemaleHair"
android:showAsAction="always"
android:title="Add Hair">
</item>
</menu>
</item>
</menu>
</item>
<item
android:id="#+id/action_settings"
android:showAsAction="always"
android:title="Sticker Settings"
android:icon="#drawable/ic_action_gear">
<menu>
<item
android:id="#+id/action_save"
android:showAsAction="always"
android:title="#string/action_save"
android:icon="#drawable/ic_action_save">
</item>
<item
android:id="#+id/action_cancel"
android:showAsAction="always"
android:title="Cancel"
android:icon="#drawable/ic_action_cancel">
</item>
<item
android:title="Sticker Settings"
android:id="#+id/action_cancel1"
android:icon="#drawable/ic_action_settings">
<menu>
<group android:checkableBehavior="single"
>
<item android:id="#+id/uniformScale"
android:title="Change Size"
android:checked="true" />
<item android:id="#+id/rotation"
android:title="Rotate"
android:checked="false" />
</group>
</menu>
</item>
</menu>
</item>
</menu>
Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dressing_room, menu);
checkCheckboxes(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.menu_femaleHat : addClothes(R.id.menu_femaleHat);
Log.d("adding hat", "hat clicked");
break;
case R.id.menu_femalePants: addClothes(R.id.menu_femalePants);
break;
case R.id.menu_femaleTop: addClothes(R.id.menu_femaleTop);
break;
case R.id.menu_maleHat: addClothes(R.id.menu_maleHat);
break;
case R.id.menu_malePants: addClothes(R.id.menu_malePants);
break;
case R.id.menu_maleTop: addClothes(R.id.menu_maleTop);
break;
case R.id.menuMaleHair: addClothes(R.id.menuMaleHair);
break;
case R.id.menuFemaleHair: addClothes(R.id.menuFemaleHair);
break;
case R.id.menuMaleAccessories: addClothes(R.id.menuMaleAccessories);
break;
case R.id.menuFemaleAccessories: addClothes(R.id.menuFemaleAccessories);
break;
case R.id.action_save: saveClicked();// cropImage();
break;
case R.id.action_cancel: goToStartScreen();// cropImage();
break;
case R.id.uniformScale:
changeAnisotropic(item);
break;
case R.id.rotation:
changeRotate(item);
break;
}
return super.onOptionsItemSelected(item);
}
private void changeRotate(MenuItem item){
if (item.isChecked()) {
item.setChecked(false);
photoSorter.setRotate(2);
photoSorter.setAnisotropic(1);
}
else{
item.setChecked(true);
photoSorter.setRotate(1);
photoSorter.setAnisotropic(2);
}
}
private void checkCheckboxes(Menu menu){
MenuItem checkBox1 = (MenuItem) menu.findItem(R.id.uniformScale);
if (checkBox1.isChecked()) {
photoSorter.setRotate(2);
photoSorter.setAnisotropic(1);
}
else{
photoSorter.setRotate(1);
photoSorter.setAnisotropic(2);
}
}
private void changeAnisotropic(MenuItem item){
if (item.isChecked()) {
item.setChecked(false);
photoSorter.setAnisotropic(2);
photoSorter.setRotate(1);
}
else{
item.setChecked(true);
photoSorter.setAnisotropic(1);
photoSorter.setRotate(2);
}
}
Nest your <items> of the second level of <menu> into <group>, for example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu1"/>
<item android:id="#+id/menu2">
<menu>
<group android:id="#+id/group2">
<item android:id="#+id/submenu1" />
<item android:id="#+id/submenu2" />
</group>
</menu>
</item>
</menu>
From the Android documentation on menus:
You can add a submenu to an item in any menu (except a submenu) by
adding a <menu> element as the child of an <item>

Categories

Resources