I have this XML.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:checked="true"
android:icon="#drawable/ic_home_black_24dp"
android:title="Home" />
<item
android:id="#+id/nav_search"
android:icon="#drawable/ic_search_black"
android:title="Search Location" />
<item
android:id="#+id/nav_fav"
android:icon="#drawable/ic_favorite"
android:title="Favorites" />
<item
android:id="#+id/nav_recent"
android:icon="#drawable/ic_nav_route"
android:title="Recent Location" />
<item
android:id="#+id/nav_route"
android:icon="#drawable/ic_place"
android:title="Route" />
</group>
<item android:title="Others">
<menu>
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings"
android:title="Settings" />
<item
android:id="#+id/nav_about"
android:icon="#android:drawable/ic_menu_send"
android:title="About" />
</menu>
</item>
</menu>
But when selecting the nav_settings,
the item is not check.
The item should be check just like the other items above.
This is my code on setting the item checked.
The fragment will just work fine.
but the selector on the nav_settings didn't work..
public void selectDrawerItem(MenuItem menuItem) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_home:
fragmentClass = FragmentMap.class;
setTitle("Map");
break;
case R.id.nav_search:
fragmentClass = FragmentSearchLoc.class;
Global.setCurrentItem =2;
break;
case R.id.nav_recent:
fragmentClass = FragmentSearchLoc.class;
Global.setCurrentItem =0;
break;
case R.id.nav_fav:
fragmentClass = FragmentSearchLoc.class;
Global.setCurrentItem =1;
break;
case R.id.nav_route:
fragmentClass = FragmentRoute.class;
setTitle("Routing");
break;
case R.id.nav_settings:
fragmentClass = FragmentSettings.class;
setTitle("Settings");
break;
default:
fragmentClass = FragmentMap.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
drawer
menuItem.setChecked(true);
drawer.closeDrawers();
}
I don't think nesting items like your code is allowed.
If you do like below, it may work:
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings"
android:title="Settings" />
<item
android:id="#+id/nav_about"
android:icon="#android:drawable/ic_menu_send"
android:title="About" />
</group>
Related
I see in a certain app that the text of top toolbar "app name" of an android app is changing when I click on any item of navigation drawer items. To relate see the pics of current task and the destination.
destination: I want to change it like this instead of the name of app
This is activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<group android:checkableBehavior="single">
<item
android:id="#+id/articles"
android:icon="#drawable/ic_menu_camera"
android:title="#string/articles" />
<item
android:id="#+id/windows"
android:icon="#drawable/ic_menu_gallery"
android:title="#string/windows" />
<item
android:id="#+id/linux"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/linux" />
<item
android:id="#+id/android"
android:icon="#drawable/ic_menu_manage"
android:title="#string/android" />
<item
android:id="#+id/miscellaneous_devices"
android:icon="#drawable/ic_menu_manage"
android:title="#string/miscellaneous_devices" />
<item
android:id="#+id/information_security"
android:icon="#drawable/ic_menu_manage"
android:title="#string/information_security" />
<item
android:id="#+id/facebook"
android:icon="#drawable/ic_menu_manage"
android:title="#string/facebook" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="Share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Send" />
</menu>
</item>
</menu>
The onNavigationItemSelected method:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.articles:
break;
case R.id.windows:
break;
case R.id.linux:
break;
case R.id.android:
break;
case R.id.miscellaneous_devices:
break;
case R.id.information_security:
break;
case R.id.facebook:
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Toolbar is just an another UI component.
Case 1: Default toolbar which coming with theme.
getSupportActionBar().setTitle("Your drawer title");
Case 2: Custom toolbar
mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("Your drawer title");
I'm trying to declare the slider items as a Button but it keeps on crashing. I'm trying to access a different Activity when an item is pressed, and trying out the Log out button. Here's the xml of the activity_main_drawer.
<group android:checkableBehavior="single">
<item
android:id="#+id/home"
android:icon="#drawable/menu_home"
android:title="Home"
/>
<item
android:id="#+id/nav_medication"
android:icon="#drawable/menu_medication"
android:title="Medication" />
<item
android:id="#+id/notices"
android:icon="#drawable/menu_notices"
android:title="Doctor's Notices" />
<item
android:id="#+id/meds"
android:icon="#drawable/menu_buy"
android:title="Buy Medicines" />
<item
android:id="#+id/options"
android:icon="#drawable/ic_menu_manage"
android:title="Medic Options">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_add"
android:icon="#drawable/menu_add"
android:title="Add Medicines" />
<item
android:id="#+id/nav_dispense"
android:icon="#drawable/menu_dispense"
android:title="Dispense" />
</group>
</menu>
</item>
</group>
<item android:title="Settings">
<menu>
<item
android:id="#+id/account"
android:icon="#drawable/menu_account"
android:title="Account" />
<item
android:id="#+id/help"
android:icon="#drawable/menu_help"
android:title="Help" />
<item
android:id="#+id/bLogout"
android:icon="#drawable/menu_help"
android:title="Logout"
/>
</menu>
</item>
Tried using:
Button bLogout;
bLogout = (Button) findViewById(R.id.bLogout);
bLogout.setOnClickListener(this);
But it still crashes.
Use this NavigationView item clicked
navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id. bLogout:
// your logic goes here
break;
}
}
});
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"/>
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"