Developer2as Developer2as - 1 year ago 133
Android Question

Navigation drawer how to highlight selected item

i use for my application the navigation drawer which you can add at the android studio and I added some menu entries to the Nav-drawer (see the code block). If I press the first item (nav_home) or the second, the nav drawer highlight the current pressed item.

If I press the item "nav_information" button or an other item, the new (clicked) fragment opend and no highlighting is visible (home or the second item is still highlighted)

Shortly, only the first items shows highlighting at selected items.

<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black"
android:title="Startseite" />

<item android:id="@+id/nav_greetings"
android:title="Grußwort"/>
</group>

<group android:checkableBehavior="single">
<item android:title="Neuigkeiten">
<menu >

<item
android:id="@+id/nav_information"
android:icon="@drawable/ic_info_black"
android:title="@string/title_activity_aktuelles" />

</menu>
</item>
</group>
....


This is the way how I handle the opening of the fragments.

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

Fragment myFragment = null;

if (id == R.id.nav_home) {
drawer.setSelected(true);
myFragment = new MainScreen();
}else if (id == R.id.nav_greetings){
myFragment = new Greetings();
} else if (id == R.id.nav_information) {
myFragment = new Aktuelles();
} else if (id == R.id.nav_directions) {
myFragment = new Anfahrt();
} else if (id == R.id.nav_basilika) {
myFragment = new Rundabout();
} else if (id == R.id.nav_download) {
myFragment = new Downloads();
} else if (id == R.id.nav_history) {
myFragment = new History();
}else if(id == R.id.nav_hauptwallfahrt){
myFragment = new Hauptwallfahrt();
}else if (id == R.id.nav_themenwallfahrt){
myFragment = new Themenwallfahrt();
}else if (id == R.id.nav_pilgergruppen){
myFragment = new Pilgergruppen();
}else if (id ==R.id.nav_wallfahrtgodi){
myFragment = new Wallfahrtsgottesdienste();
}


FragmentManager fragmentManager = getSupportFragmentManager();

FragmentTransaction ft = fragmentManager.beginTransaction();

// Replace the requestet Fragment
ft.replace(R.id.container, myFragment) ;
ft.commit();

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}


Is there an way to "enable" the highlighting?

Thanks in advance!

Answer Source

you only have to change the order of the tags

I tried with the last group but it works, check it pls

<item android:title="Neuigkeiten">
  <menu >
      <group android:checkableBehavior="single">
          <item
            android:id="@+id/nav_information"
            android:icon="@drawable/ic_info_black"
            android:title="@string/title_activity_aktuelles" />

     </group>
 </menu>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download