I have the following activity_nav_drawer_drawer.xml as the app:menu for a NavigationView in a DrawerLayout.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/nav_group_features"
android:checkableBehavior="single">
<item android:id="#+id/nav_feature1"
android:icon="#drawable/ic_build_black"
android:title="Feature 1"/>
<item android:id="#+id/nav_feature2"
android:icon="#drawable/ic_account_balance_black"
android:title="Feature 2"/>
<item android:id="#+id/nav_feature3"
android:icon="#drawable/ic_folder_black"
android:title="Feature 3"/>
</group>
<item android:title="Select Project">
<group android:id="#+id/nav_group_projects"
android:checkableBehavior="single">
<item android:id="#+id/nav_project1"
android:icon="#drawable/ic_domain_black"
android:title="Project 1"/>
<item android:id="#+id/nav_project2"
android:icon="#drawable/ic_domain_black"
android:title="Project 2"/>
<item android:id="#+id/nav_project3"
android:icon="#drawable/ic_domain_black"
android:title="Project 3"/>
</group>
</item>
</menu>
Here is the containing DrawerLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:id="#+id/drawer_layout"
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include layout="#layout/app_bar_nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_nav_drawer"
app:menu="#menu/activity_nav_drawer_drawer"/>
</android.support.v4.widget.DrawerLayout>
The activity implements NavigationView.OnNavigationItemSelectedListener with the flowing listener.
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
return true;
}
The problem is if one item is selected from one group, it un-selects the selected item in the other group. I need each groups to have an independent selection. How do you do this?
For me, it looks like a bug in NavigationView itself and there're several similar issues (like this one or this) in Google's bugtracker regarding it.
However, you can archive it, by doing this:
Keep track of previously selected MenuItems from each group
once item clicked - uncheck previously checked element of the group and check the new one
Remove android:checkableBehavior from the xml and add android:checkable="true" for each MenuItem
There's actual implementation:
MenuItem selectedFeature, selectedProject;
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
if (item.getGroupId() == R.id.group_feature) {
if (selectedFeature != null) {
selectedFeature.setChecked(false);
}
selectedFeature = item;
selectedFeature.setChecked(true);
} else if (item.getGroupId() == R.id.group_projects) {
if (selectedProject != null) {
selectedProject.setChecked(false);
}
selectedProject = item;
selectedProject.setChecked(true);
}
return false;
}
And activity_main_drawer.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/group_feature">
<item android:id="#+id/nav_feature1"
android:icon="#drawable/ic_menu_camera"
android:checkable="true"
android:title="Feature 1"/>
<item android:id="#+id/nav_feature2"
android:icon="#drawable/ic_menu_gallery"
android:checkable="true"
android:title="Feature 2"/>
<item android:id="#+id/nav_feature3"
android:icon="#drawable/ic_menu_slideshow"
android:checkable="true"
android:title="Feature 3"/>
</group>
<item android:title="Select Project">
<menu>
<group
android:id="#+id/group_projects">
<item android:id="#+id/nav_project1"
android:icon="#drawable/ic_menu_manage"
android:checkable="true"
android:title="Project 1"/>
<item android:id="#+id/nav_project2"
android:icon="#drawable/ic_menu_share"
android:checkable="true"
android:title="Project 2"/>
<item android:id="#+id/nav_project3"
android:icon="#drawable/ic_menu_send"
android:checkable="true"
android:title="Project 3"/>
</group>
</menu>
</item>
</menu>
I hope, it helps
Related
I have a list of items as follows: (act_pedido_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"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_inicio"
android:title="#string/nav_inicio"/>
</group>
<item
android:title="#string/nav_empresa"
android:id="#+id/nav_mi_empresa_title">
<menu>
<item
android:id="#+id/nav_mi_empresa"
android:title="#string/nav_mi_empresa"/>
<item
android:id="#+id/nav_inventario"
android:title="#string/nav_inventario"/>
<item
android:id="#+id/nav_descuentos"
android:title="#string/nav_descuentos"/>
<item
android:id="#+id/nav_clientes_proveedores"
android:title="#string/nav_clientes_proveedores"/>
<item
android:id="#+id/nav_ventas"
android:title="#string/nav_ventas"/>
<item
android:id="#+id/nav_reportes"
android:title="#string/nav_reportes"/>
<item
android:id="#+id/nav_suscribirse"
android:title="#string/nav_suscribirse"/>
</menu>
</item>
<item android:title="#string/nav_cuenta">
<menu>
<item
android:id="#+id/nav_gastos"
android:title="#string/nav_gastos"/>
<item
android:id="#+id/nav_impuestos"
android:title="#string/nav_impuestos"/>
<item
android:id="#+id/nav_hardware"
android:title="#string/nav_hardware"/>
<item
android:id="#+id/nav_terminos_condiciones"
android:title="#string/nav_terminos_condiciones"/>
<item
android:id="#+id/nav_cerrar_sesion"
android:title="#string/nav_cerrar_sesion"/>
</menu>
</item>
</menu>
Which has a similar appearance to the following image:
The list is invoked from the following xml file, (app:menu="#menu/act_pedido_drawer"):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/act_bar_pedido"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_cabecera_act_pedido"
app:menu="#menu/act_pedido_drawer"/>
</android.support.v4.widget.DrawerLayout>
The way to determine which item has been selected is as follows:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_mi_empresa_title:{
break;
}
case R.id.nav_terminos_condiciones: {
break;
}
case R.id.nav_mi_empresa: {
break;
}
case R.id.nav_inventario: {
break;
}
case R.id.nav_descuentos: {
break;
}
case R.id.nav_clientes_proveedores: {
break;
}
case R.id.nav_ventas:{
break;
}
case R.id.nav_cerrar_sesion: {
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
For all the items it works correctly, except for the item nav_mi_empresa_title which can be seen in the image that seems to be disabled.
My question is: Is there a way to enable it?
Thanks
Try this code, I simply put these two items inside menu:
<?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_inicio"
android:title="#string/nav_inicio"/>
</group>
<item
android:title="#string/nav_empresa">
<menu>
<item
android:title="#string/nav_empresa"
android:id="#+id/nav_mi_empresa_title"/>
<item
android:id="#+id/nav_mi_empresa"
android:title="#string/nav_mi_empresa"/>
<item
android:id="#+id/nav_inventario"
android:title="#string/nav_inventario"/>
<item
android:id="#+id/nav_descuentos"
android:title="#string/nav_descuentos"/>
<item
android:id="#+id/nav_clientes_proveedores"
android:title="#string/nav_clientes_proveedores"/>
<item
android:id="#+id/nav_ventas"
android:title="#string/nav_ventas"/>
<item
android:id="#+id/nav_reportes"
android:title="#string/nav_reportes"/>
<item
android:id="#+id/nav_suscribirse"
android:title="#string/nav_suscribirse"/>
</menu>
</item>
<item android:title="#string/nav_cuenta">
<menu>
<item
android:id="#+id/nav_cuenta"
android:title="#string/nav_cuenta">
<item
android:id="#+id/nav_gastos"
android:title="#string/nav_gastos"/>
<item
android:id="#+id/nav_impuestos"
android:title="#string/nav_impuestos"/>
<item
android:id="#+id/nav_hardware"
android:title="#string/nav_hardware"/>
<item
android:id="#+id/nav_terminos_condiciones"
android:title="#string/nav_terminos_condiciones"/>
<item
android:id="#+id/nav_cerrar_sesion"
android:title="#string/nav_cerrar_sesion"/>
</menu>
</item>
</menu>
I will continue with the only idea suggested in this thread, but adding some lines of separation in some items.
<?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:id="#+id/grp1" android:checkableBehavior="single">
<item
android:id="#+id/nav_inicio"
android:title="#string/nav_inicio"/>
</group>
<group android:id="#+id/grp2" android:checkableBehavior="single">
<item
android:color="#color/colorDeshabilitado"
android:id="#+id/nav_mi_empresa_titulo"
android:title="#string/nav_empresa"/>
</group>
<group android:id="#+id/grp3" android:checkableBehavior="single">
<item
android:id="#+id/nav_mi_empresa"
android:title="#string/nav_mi_empresa"/>
<item
android:id="#+id/nav_inventario"
android:title="#string/nav_inventario"/>
<item
android:id="#+id/nav_descuentos"
android:title="#string/nav_descuentos"/>
<item
android:id="#+id/nav_clientes_proveedores"
android:title="#string/nav_clientes_proveedores"/>
<item
android:id="#+id/nav_ventas"
android:title="#string/nav_ventas"/>
<item
android:id="#+id/nav_reportes"
android:title="#string/nav_reportes"/>
<item
android:id="#+id/nav_suscribirse"
android:title="#string/nav_suscribirse"/>
</group>
<item
android:title="#string/nav_cuenta"
android:id="#+id/nav_titulo_cuenta">
<menu>
<item
android:id="#+id/nav_gastos"
android:title="#string/nav_gastos"/>
<item
android:id="#+id/nav_impuestos"
android:title="#string/nav_impuestos"/>
<item
android:id="#+id/nav_hardware"
android:title="#string/nav_hardware"/>
<item
android:id="#+id/nav_terminos_condiciones"
android:title="#string/nav_terminos_condiciones"/>
<item
android:id="#+id/nav_cerrar_sesion"
android:title="#string/nav_cerrar_sesion"/>
</menu>
</item>
</menu>
Thanks to #D.B. for answering.
I implemented the Navigation View (Support Library 24.0.0):
But a strange behavior occurred in which i have been trying to control since.
Here is my drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Categories">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_1"
android:icon="#drawable/ic_news"
android:title="Top Stories" />
<item
android:id="#+id/navigation_item_2"
android:icon="#drawable/ic_music"
android:title="Entertainment" />
<item
android:id="#+id/navigation_item_3"
android:icon="#drawable/ic_sport"
android:title="Sport" />
<item
android:id="#+id/navigation_item_4"
android:icon="#drawable/ic_lifestyle"
android:title="Lifestyle" />
<item
android:id="#+id/navigation_item_5"
android:icon="#drawable/ic_tech"
android:title="Technology" />
<item
android:id="#+id/navigation_item_6"
android:icon="#drawable/ic_world"
android:title="International" />
</group>
</menu>
</item>
</menu>
But the result is :
in which only one should be selected.
I solved it using :
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
for (int i = 0; i < 6; i++) {
MenuItem top = navigationView.getMenu().getItem(0).getSubMenu().getItem(i);
if (top.isChecked()) top.setChecked(false);
}
return true;
}
});
Unchecking all items in the menu
return true, that checks the clicked item.
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 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" />
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