Having trouble adding extra items to my options menu Android - android

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"

Related

drawable states won't update on action bar

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?

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"/>

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>

handle clicks on submenus in android

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.

Android Menu: Two single-checkable groups in the same menu

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'

Categories

Resources