My xml code is:
<group android:checkableBehavior="single">
<item android:id="#+id/group_1_box_1"/>
<item android:id="#+id/group_1_box_2"/>
</group>
<group android:checkableBehavior="single">
<item android:id="#+id/group_2_box_1"/>
<item android:id="#+id/group_2_box_2"/>
</group>
The problem I am having is that when I change the state of group_1_box_1 to true, it not only sets group_1_box_2 to false, it also sets the group_2 boxes to false.
Edit: Here's my code
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.group_1_box_1:
item.setChecked(true);
return true;
case R.id.group_1_box_2:
item.setChecked(true);
return true;
case R.id.group_2_box_1:
item.setChecked(true);
return true;
case R.id.group_2_box_2:
item.setChecked(true);
return true;
default:
return super.onOptionsItemSelected(item);
}
You just need to add unique IDs to each of the group items in the xml file.
<group
android:id="#+id/menuGroup_1" <!-- Here -->
android:checkableBehavior="single">
<item android:id="#+id/group_1_box_1"/>
<item android:id="#+id/group_1_box_2"/>
</group>
<group
android:id="#+id/menuGroup_2" <!-- And here -->
android:checkableBehavior="single">
<item android:id="#+id/group_2_box_1"/>
<item android:id="#+id/group_2_box_2"/>
</group>
Use:
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
Related
I have an action bar which contains an item shuffle_action.
I have used below case in the onOptionsItemSelected(MenuItem item) function.
switch (item.getItemId()){
case R.id.action_shuffle:
if(MusicService.shuffle) {
item.setChecked(false);
Toast.makeText(this,String.valueOf(item.isChecked()),
Toast.LENGTH_SHORT).show();// returns false
musicService.clearShuffle();
}
else {
item.setChecked(true);
Toast.makeText(this,String.valueOf(item.isChecked()),
Toast.LENGTH_SHORT).show();// returns true
}
musicService.setShuffle();
break;
}
Now this thing works perfectly as i want it to work.
<item
android:id="#+id/action_shuffle"
android:icon="#drawable/shuffle_drawable"
android:title="Shuffle"
app:showAsAction="ifRoom"
android:checkable="true"
android:checked="false"
/>
shuffle_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/shuffle_pressed"/>
<item
android:state_checked="false"
android:drawable="#drawable/ic_shuffle_black_18dp"/>
</selector>
The thing is that when the state changes the drawable won't change.Why is this happening?
I am very new to Java Programming and Android Studio.
While creating an Options Menu, I created a list of options in the main menu with two items in a group sub menu.
This is my menu.xml
<?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/giveup_setting"
android:orderInCategory="1"
app:showAsAction="ifRoom"
android:title="#string/action_option1"/>
<item
android:id="#+id/new_game_setting"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/action_option2"/>
<item
android:id="#+id/help_setting"
android:orderInCategory="3"
app:showAsAction="never"
android:title="#string/action_option3"/>
<item
android:id="#+id/settings_setting"
android:orderInCategory="4"
app:showAsAction="never"
android:title="#string/action_option4" />
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/menu_color"
android:orderInCategory="1"
app:showAsAction="never"
android:title="#string/layout_color"/>
<item
android:id="#+id/menu_text"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/layout_text"/>
</group>
</menu>
<item
android:id="#+id/about_setting"
android:orderInCategory="5"
app:showAsAction="never"
android:title="#string/action_option5"/>
<item
android:id="#+id/exit_setting"
android:orderInCategory="6"
app:showAsAction="never"
android:title="#string/action_option6"/>
</menu>
An this is how I inflate the menu in my MainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
menu.clear();
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menu) {
// Handle item selection
switch (menu.getItemId()) {
case R.id.help_setting:
//show help screens
return true;
case R.id.menu_color:
//change a layout of my main activity
return true;
case R.id.menu_text:
//change another layout of my main activity
return true;*/
case R.id.about_setting:
//show my about screen
return true;
case R.id.giveup_setting:
//resets the game
case R.id.new_game_setting:
//restarts the app
return true;
case R.id.exit_setting:
//exits the app
return true;
default:
return super.onOptionsItemSelected(menu);
}
}
On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.
On running the app, the menu populates but the "Settings" item (which has the nested group radio buttons) displays twice.
What am I doing wrong?
if android:id="#+id/settings_setting" is the one you want to have a submenu don't close the item tag. Instead inflate <menu> inside the <item> tag.
<item
android:id="#+id/settings_setting"
android:orderInCategory="4"
app:showAsAction="never"
android:title="#string/action_option4">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/menu_color"
android:orderInCategory="1"
app:showAsAction="never"
android:title="#string/layout_color"/>
<item
android:id="#+id/menu_text"
android:orderInCategory="2"
app:showAsAction="never"
android:title="#string/layout_text"/>
</group>
</menu>
</item>
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>
A the moment I have this options menu
I would like to add to options at the bottom below hybrid map. Here is my code. I have added in where I would like the two other menu options to be but they don't work.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.normal_map:
mapType=GoogleMap.MAP_TYPE_NORMAL;
break;
case R.id.satellite_map:
mapType=GoogleMap.MAP_TYPE_SATELLITE;
break;
case R.id.terrain_map:
mapType=GoogleMap.MAP_TYPE_TERRAIN;
break;
case R.id.hybrid_map:
mapType=GoogleMap.MAP_TYPE_HYBRID;
break;
case R.id.games2014:
games2014=true;
break;
case R.id.games_past:
games2014=false;
break;
}
googleMap.setMapType(mapType);
return true;
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/map_types"
android:title="Menu"
android:showAsAction="always">
<menu>
<item android:id="#+id/normal_map"
android:title="Mormal map"/>
<item android:id="#+id/satellite_map"
android:title="Satellite map"/>
<item android:id="#+id/terrain_map"
android:title="Terrain map"/>
<item android:id="#+id/hybrid_map"
android:title="Hybrid map"/>
<Item android:id="#+id/games2014"
android:title="2014 Games"/>
<Item android:id="#+id/games_past"
android:title="Previous Games"/>
</menu>
</item>
In your item you declared it as Item which would cause it not to add
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.normal_map:
mapType=GoogleMap.MAP_TYPE_NORMAL;
break;
case R.id.satellite_map:
mapType=GoogleMap.MAP_TYPE_SATELLITE;
break;
case R.id.terrain_map:
mapType=GoogleMap.MAP_TYPE_TERRAIN;
break;
case R.id.hybrid_map:
mapType=GoogleMap.MAP_TYPE_HYBRID;
break;
case R.id.games2014:
games2014=true;
break;
case R.id.games_past:
games2014=false;
break;
}
googleMap.setMapType(mapType);
return true;
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/map_types"
android:title="Menu"
android:showAsAction="always">
<menu>
<item android:id="#+id/normal_map"
android:title="Mormal map"/>
<item android:id="#+id/satellite_map"
android:title="Satellite map"/>
<item android:id="#+id/terrain_map"
android:title="Terrain map"/>
<item android:id="#+id/hybrid_map"
android:title="Hybrid map"/>
<item android:id="#+id/games2014"
android:title="2014 Games"/>
<item android:id="#+id/games_past"
android:title="Previous Games"/>
</menu>
</item>
<Item android:id="#+id/games2014"
android:title="2014 Games"/>
<Item android:id="#+id/games_past"
android:title="Previous Games"/>
Your "I" in "Item" is capital. It should be a lowercase "i" - "item"
I am new to android and stuck at the point where i have to detect clicks on submenus that are defined in XML file
my XML file is :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ccard_menu1"
android:title="Select from Profile?"
></item>
<item android:id="#+id/ccard_menu2"
android:title="Add Field"
>
<menu >
<item android:id="#+id/submenu1"
android:title="Add Products"
></item>
<item android:id="#+id/submenu2"
android:title="Add Clients"
></item>
<item android:id="#+id/submenu3"
android:title="Add a Custom Field">s</item>
</menu>
</item>
</menu>
how do i detect clicks on "submenu 1,2,3" in onOptionsItemSelected method?
how do i have to structure the switch case?
I you are looking for something like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.submenu1:
// do something
return true;
case R.id.submenu2:
//do something else
return true;
// etc..
default:
return super.onOptionsItemSelected(item);
}
}
Please correct me if I am mistaken.