Navigation Drawer menu items selected within different groups - java

I have a working Navigation Drawer and having some issues with menuItem.setChecked(true); when using groups and headers within the menu. It's not highlighting menu items as expected.
Here is my XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ActivityMap">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_welcome"
android:icon="#drawable/abc_btn_check_to_on_mtrl_000"
android:title="Welcome" />
<item
android:id="#+id/nav_map_showmap"
android:icon="#mipmap/ic_map_black_24dp"
android:title="Show Map" />
<item
android:icon="#mipmap/ic_list_black_24dp"
android:title="Show List" />
<item android:title="Settings">
<menu>
<item
android:id="#+id/nav_database_check"
android:icon="#mipmap/ic_cloud_done_black_24dp"
android:title="Update Database" />
<item
android:id="#+id/nav_map_settings"
android:icon="#mipmap/ic_settings_black_24dp"
android:title="App Preferences" />
</menu>
</item>
<item android:title="General">
<menu>
<item
android:id="#+id/nav_general_about"
android:icon="#mipmap/ic_info_black_24dp"
android:title="About" />
<item
android:id="#+id/nav_general_help"
android:icon="#mipmap/ic_help_black_24dp"
android:title="Help" />
<item
android:id="#+id/nav_general_report"
android:icon="#mipmap/ic_email_black_24dp"
android:title="Feedback / Report error" />
</menu>
</item>
</group>
</menu>
As you can see I have 3 menu items, then a Settings Group with 2 menu items, then a General Group with 3 menu items.
Now with the first 3 menu items, the menuItem.setChecked(true); is working as expected, and highlighting that menu item. However, none of the following menu items within the subset ("Settings" or "General" subsets) are highlighting correctly.
I read that the <group android:checkableBehaviour="single"> can encapsulate the whole block, but that doesn't seem to be working.
Any thoughts? Cheers.
EDIT - Added Screenshot of the menu structure

UPDATE
If you have upgraded your Android Studio to 1.4, you can create a new project (with Navigation Drawer Activity template). Then, you can update your menu file as the following (pay attention to the two lines <group android:checkableBehavior="single"> inside 2 sub-menus at "Communicate" and "BNK"):
<?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_camara" android:icon="#android:drawable/ic_menu_camera"
android:title="Import" />
<item android:id="#+id/nav_gallery" android:icon="#android:drawable/ic_menu_gallery"
android:title="Gallery" />
<item android:id="#+id/nav_slideshow" android:icon="#android:drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item android:id="#+id/nav_manage" android:icon="#android:drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<group android:checkableBehavior="single">
<item android:id="#+id/nav_share" android:icon="#android:drawable/ic_menu_share"
android:title="Share" />
<item android:id="#+id/nav_send" android:icon="#android:drawable/ic_menu_send"
android:title="Send" />
</group>
</menu>
</item>
<item android:title="BNK">
<menu>
<group android:checkableBehavior="single">
<item android:id="#+id/nav_share_bnk" android:icon="#android:drawable/ic_menu_share"
android:title="Share BNK" />
<item android:id="#+id/nav_send_bnk" android:icon="#android:drawable/ic_menu_send"
android:title="Send BNK" />
</group>
</menu>
</item>
</menu>
And here is the screenshot
END OF UPDATE
As I have commented, please try putting <group android:checkableBehavior="single"> below menus of Settings and General instead of putting below the outside menu. That means you will have 2 groups. Hope this helps!

<group android:checkableBehavior="single">
<item
android:id="#+id/nav_vi_tri"
android:icon="#drawable/ic_menu_camera"
android:title="Vị trí" />
<item
android:id="#+id/nav_xem_lai"
android:icon="#drawable/ic_menu_gallery"
android:title="Xem lại hành trình" />
<item
android:id="#+id/nav_hinh_anh"
android:icon="#drawable/ic_menu_slideshow"
android:title="Hình ảnh" />
<item android:title="Báo cáo">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_baocao1"
android:icon="#drawable/ic_menu_camera"
android:title="Hành trình xe chạy" />
<item
android:id="#+id/nav_baocao2"
android:icon="#drawable/ic_menu_gallery"
android:title="Tốc độ của xe" />
<item
android:id="#+id/nav_baocao3"
android:icon="#drawable/ic_menu_slideshow"
android:title="Quá tốc độ" />
<item
android:id="#+id/nav_baocao4"
android:icon="#drawable/ic_menu_slideshow"
android:title="Thời gian lái" />
<item
android:id="#+id/nav_baocao5"
android:icon="#drawable/ic_menu_slideshow"
android:title="Dừng đỗ" />
<item
android:id="#+id/nav_baocao6"
android:icon="#drawable/ic_menu_slideshow"
android:title="Tổng hợp theo xe" />
<item
android:id="#+id/nav_baocao7"
android:icon="#drawable/ic_menu_slideshow"
android:title="Quãng đường nhiên liệu" />
</group>
</menu>
</item>
<item android:title="Communicate">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_tien_ich"
android:icon="#drawable/ic_menu_share"
android:title="Tiện ích" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_menu_send"
android:title="Đăng xuất" />
</group>
</menu>
</item>
</group>
it work for me :D

Here you can use three different groups top , center and bottom. And first top group will have 3 menu items as you have . Then the center group can be same as your "Settings" menu with 2 items. And bottom group will have 2 items as it is in General.
And in your java file you can handle Item Selected Listener to handle the item click of the navigation menu.
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(MenuItem p_menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(p_menuItem.getGroupId()==R.id.menu_top){
m_navigationView.getMenu().setGroupCheckable(R.id.menu_bottom,false,true);
m_navigationView.getMenu().setGroupCheckable(R.id.menu_center,false,true);
m_navigationView.getMenu().setGroupCheckable(R.id.menu_top,true,true);
}
else if(p_menuItem.getGroupId()==R.id.menu_center){
m_navigationView.getMenu().setGroupCheckable(R.id.menu_bottom,false,true);
m_navigationView.getMenu().setGroupCheckable(R.id.menu_center,true,true);
m_navigationView.getMenu().setGroupCheckable(R.id.menu_top,false,true);
}
else{
m_navigationView.getMenu().setGroupCheckable(R.id.menu_bottom,true,true);
m_navigationView.getMenu().setGroupCheckable(R.id.menu_center,false,true);
m_navigationView.getMenu().setGroupCheckable(R.id.menu_top,false,true);
}
p_menuItem.setChecked(true);
m_drawerLayout.closeDrawers();

Try this:
private int selectedItem = 0;
private void setSelectedItem(MenuItem menuItem, int position) {
mNavigationView.getMenu().getItem(selectedItem).setChecked(false);
mNavigationView.getMenu().getItem(position).setChecked(true);
if (menuItem.getGroupId() == R.id.group_2) {
mNavigationView.getMenu().setGroupCheckable(R.id.group_1, false, true);
mNavigationView.getMenu().setGroupCheckable(R.id.group_2, true, true);
} else {
mNavigationView.getMenu().setGroupCheckable(R.id.group_1, true, true);
mNavigationView.getMenu().setGroupCheckable(R.id.group_2, false, true);
}
//Update highlighted item in the navigation menu
menuItem.setChecked(true);
selectedItem = position;
}

Change the position on closing group tag </group> in your code as under:
Look the position carefully and do it.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ActivityMap">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_welcome"
android:icon="#drawable/abc_btn_check_to_on_mtrl_000"
android:title="Welcome" />
<item
android:id="#+id/nav_map_showmap"
android:icon="#mipmap/ic_map_black_24dp"
android:title="Show Map" />
<item
android:icon="#mipmap/ic_list_black_24dp"
android:title="Show List" />
</group>
<item android:title="Settings">
<menu>
<item
android:id="#+id/nav_database_check"
android:icon="#mipmap/ic_cloud_done_black_24dp"
android:title="Update Database" />
<item
android:id="#+id/nav_map_settings"
android:icon="#mipmap/ic_settings_black_24dp"
android:title="App Preferences" />
</menu>
</item>
<item android:title="General">
<menu>
<item
android:id="#+id/nav_general_about"
android:icon="#mipmap/ic_info_black_24dp"
android:title="About" />
<item
android:id="#+id/nav_general_help"
android:icon="#mipmap/ic_help_black_24dp"
android:title="Help" />
<item
android:id="#+id/nav_general_report"
android:icon="#mipmap/ic_email_black_24dp"
android:title="Feedback / Report error" />
</menu>
</item>
</menu>

Related

How to change menu title programmatically in Android

Here is my menu:
<?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/home"
android:icon="#drawable/home"
android:title="#string/bottom_basic_home" />
<item
android:id="#+id/mancust"
android:icon="#drawable/ic_contacts1"
android:title="#string/customers" />
<item
android:id="#+id/catalog"
android:icon="#drawable/catalog"
android:title="#string/catalog" />
</group>
<item android:title="About"> //my changes want to appear for this item
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/help"
android:icon="#drawable/ic_help_outline_black_24dp"
android:title="#string/help"
/>
<item
android:id="#+id/codex_logout"
android:icon="#drawable/logout"
android:title="#string/logout" />
</group>
</menu>
</item>
</menu>
I am implementing navigation drawer in my Android application. How do I set a menu group item title programmatically?
You can add id tag to your item in xml and find that item in the menu programmatically and set a new title.
Menu XML:
<item
android:id="#+id/about_title"
android:title="About">
Java/Kotlin:
val navView: NavigationView = findViewById(R.id.nav_view)
val aboutTitle = navView.menu.findItem(R.id.about_title)
aboutTitle.title = "New Title"

set Image(PNG etc) as an icon on drawer layout navigation items

I'm setting the image as icon but its showing grey icon not the image,
This #drawable/image_second is the image I want to use as icon.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/image_second"
android:title="#string/menu_home" />
<item
android:id="#+id/nav_gallery"
android:icon="#drawable/ic_menu_gallery"
android:title="#string/menu_gallery" />
<item
android:id="#+id/nav_slideshow"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/menu_slideshow" />
<item
android:id="#+id/nav_tools"
android:icon="#drawable/ic_menu_manage"
android:title="#string/menu_tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="#string/menu_share" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="#string/menu_send" />
</menu>
</item>
</menu>
after running the code,I get this,
please tell what to do and why its happening..
As always thanks a lot..
You can do so programmatically using
NavigationView.setItemIconTintList.
yourNavigationView.setItemIconTintList(null);
Also If you want to change the tint of other icons use this
In XML
<android.support.design.widget.NavigationView
...
app:itemIconTint="#android:color/black"
... />
Try removing the item icon tint.
mNavigationView.setItemIconTintList(null);

Menu items showing twice

XML:
<?xml version="1.0" encoding="utf-8"?>
<group>
<item
android:id="#+id/share"
android:title="Share"/>
<menu>
<item android:title="Share Contact"
android:id="#+id/sharecontact" />
<item android:title="Share Profile"
android:id="#+id/shareprofile"/>
<item android:title="Share Conversation"
android:id="#+id/sharechat"/>
</menu>
</group>
<group>
<item
android:id="#+id/options"
android:title="Options"/>
<menu>
<item android:title="Mute Notifications"
android:id="#+id/mutenotifications" />
<item android:title="Pin"
android:id="#+id/pin" />
<item android:title="Archieve Chat"
android:id="#+id/archievechat" />
<item android:title="Create Shortcut"
android:id="#+id/createshortcut" />
<item android:title="Clear Chat"
android:id="#+id/clearchat" />
<item android:title="Backup Chat"
android:id="#+id/backupchat" />
</menu>
</group>
<group>
<item
android:id="#+id/more"
android:title="More"/>
<menu>
<item android:title="Ignore Contact"
android:id="#+id/ignorecontact" />
<item android:title="Report Contact"
android:id="#+id/reportcontact" />
<item android:title="Delete Contact"
android:id="#+id/deletecontact" />
<item android:title="Block Contact"
android:id="#+id/blockcontact" />
</menu>
</group>
<group>
<item
android:id="#+id/privacy"
android:title="Privacy"/>
<menu>
<item android:title="Screenshot Awareness"
android:id="#+id/screenshotawareness" />
<item android:title="Deleting Messages Awareness"
android:id="#+id/deletingmessagesawareness" />
<item android:title="LastSeen"
android:id="#+id/lastseen" />
<item android:title="Online"
android:id="#+id/online" />
<item android:title="Seen"
android:id="#+id/seen" />
<item android:title="Copy Awareness"
android:id="#+id/copy" />
</menu>
</group>
Java code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.profile, menu);
return true;
}
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Fragment fragment = null;
if (id == R.id.share) {
} else if (id == R.id.options) {
} else if (id == R.id.privacy) {
} else if (id == R.id.more) {
}
return true;
}
Why are the menu items showing twice instead of just once? I did look into the code from Google but it looks the same.
You must start the menu with the <menu> tag and the hierarchy must go something like this:
<?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:title="#string/trip_details">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_dashboard"
android:icon="#drawable/dashboard1_icons"
android:title="#string/dashboard"
android:checked="true"/>
<item
android:id="#+id/nav_my_trip"
android:icon="#drawable/ic_trip_icon"
android:title="#string/my_trip" />
<item
android:id="#+id/nav_expenses"
android:icon="#drawable/ic_expense"
android:title="#string/expense" />
</group>
</menu>
</item>
<item android:title="#string/help_and_support">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_help"
android:icon="#drawable/ic_help"
android:title="#string/helpdesk" />
<item
android:id="#+id/nav_change_language"
android:icon="#drawable/ic_language"
android:title="#string/change_language"
android:visible="true"/>
<item
android:id="#+id/nav_logout"
android:icon="#drawable/logout_icons"
android:title="#string/log_out" />
<item
android:id="#+id/nav_version_name"
android:title=""
app:actionLayout="#layout/menu_version_view" />
</group>
</menu>
</item>
</menu>
This is just an example of my menu with groups.
Try it. Please update here if it works.
You should put all the group and menu tags inside the main item tag. Your menu.xml should like this:
<item
android:id="#+id/share"
android:title="Share">
<menu>
<group>
<item
android:id="#+id/sharecontact"
android:title="Share Contact" />
<item
android:id="#+id/shareprofile"
android:title="Share Profile" />
<item
android:id="#+id/sharechat"
android:title="Share Conversation" />
</group>
</menu>
...
</item>
And so on...

Actions aren't showing on Toolbar with "ifRoom"

I have problem with actions on Toolbar in my Android app.
I have a group with some actions with app:showAsAction="ifRoom" attribute. Everything works fine if there are fewer than 4 items with ifRoom. But when I add fourth item, then all the items disappear from toolbar.
It works fine:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/group_paint"
android:orderInCategory="1"
app:showAsAction="ifRoom">
<item android:id="#+id/action_layers"
android:icon="#drawable/ic_layers_white_24dp"
android:title="#string/action_layers"
android:orderInCategory="3"
app:showAsAction="ifRoom"/>
<item android:id="#+id/action_tool"
android:icon="#drawable/ic_properties_white_24dp"
android:title="#string/action_tool"
android:orderInCategory="4"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_action1"
android:icon="#drawable/ic_action1_white_24dp"
android:title="#string/action_1"
android:orderInCategory="7"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_settings_white_24dp"
android:title="#string/action_settings"
android:orderInCategory="7"
app:showAsAction="never" />
</group>
</menu>
Unexpected result:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:id="#+id/group_paint"
android:orderInCategory="1"
app:showAsAction="ifRoom">
<item android:id="#+id/action_layers"
android:icon="#drawable/ic_layers_white_24dp"
android:title="#string/action_layers"
android:orderInCategory="3"
app:showAsAction="ifRoom"/>
<item android:id="#+id/action_tool"
android:icon="#drawable/ic_properties_white_24dp"
android:title="#string/action_tool"
android:orderInCategory="4"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_action1"
android:icon="#drawable/ic_action1_white_24dp"
android:title="#string/action_1"
android:orderInCategory="7"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_action2"
android:icon="#drawable/ic_action2_white_24dp"
android:title="#string/action_2"
android:orderInCategory="8"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_settings_white_24dp"
android:title="#string/action_settings"
android:orderInCategory="7"
app:showAsAction="never" />
</group>
</menu>
What I would like to see is: three actions on toolbar and the fourth in the overflow menu.
You are using the <group> element to treat the items as a group. If you do not want to treat the items as a group (e.g., you want them to appear in the action bar individually based upon available space), get rid of the <group> element.

Android - Find id of item in SubMenu

I use Navigation activities for moving from one Activity to another. And I want to add submenu to menu of NavigationView< but can't find way to find id og item in submenu.
<menu>
<item android:title="Communicate">
<menu>
<item android:id="#+id/about_us" android:icon="#android:drawable/ic_menu_share"
android:title="About us" />
</menu>
</item></menu>
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id) {
case R.id.nav_order: {
break;
}
case R.id.nav_search: {
break;
}
}
return true;
}
I want to add: case R.id.about_us.
But Android Studio can't find this id.
How can i make it and what i do wrong?
Your code worked fine for me. There is one thing missing in your menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"/> <!--the xmlns is missing -->
</menu>
try to write like this:
The drawer itesm below have been classified into groups
<group
android:id="#+id/group_1"
android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_1"
android:icon="#drawable/ic_number_0"
android:title="#string/navigation_item_1" />
<item
android:id="#+id/navigation_item_2"
android:icon="#drawable/ic_number_1"
android:title="#string/navigation_item_2" />
</group>
<group
android:id="#+id/group_2"
android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_3"
android:icon="#drawable/ic_number_2"
android:title="#string/navigation_item_3" />
<item
android:id="#+id/navigation_item_4"
android:icon="#drawable/ic_number_3"
android:title="#string/navigation_item_4" />
</group>
<group
android:id="#+id/group_3"
android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_5"
android:icon="#drawable/ic_number_4"
android:title="#string/navigation_item_5" />
</group>
OR
The drawer items below have been categorized according to headers
<item
android:id="#+id/navigation_item_1"
android:icon="#drawable/ic_number_0"
android:title="#string/navigation_item_1">
<menu>
<item
android:id="#+id/navigation_item_2"
android:icon="#drawable/ic_number_1"
android:title="#string/navigation_item_2" />
<item
android:id="#+id/navigation_item_3"
android:icon="#drawable/ic_number_2"
android:title="#string/navigation_item_3" />
</menu>
</item>
<item
android:id="#+id/navigation_item_4"
android:icon="#drawable/ic_number_3"
android:title="#string/navigation_item_4">
<menu>
<item
android:id="#+id/navigation_item_5"
android:icon="#drawable/ic_number_4"
android:title="#string/navigation_item_5" />
</menu>
</item>

Categories