Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Drawer: Add Titles to Groups, Not Items

Tags:

I have a standard Navigation Drawer, pre-created by Android Studio and want to populate it with number of groups. I started with this:

<?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_mode_person"             android:icon="@drawable/ic_person_black_24dp"             android:title="Person" />         <item             android:id="@+id/nav_mode_group"             android:icon="@drawable/ic_group_black_24dp"             android:title="Community" />     </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> 

But what I didn't get is if it even possible to give each group a title? I mean there is an android:title option available for <item>s, but it is unavailable for <group>s and if I try to wrap groups around items, what I get is a messy entries behavior.

I read through Google's design guideline on Navigation Drawer, but missed the point if groups should have its own names or they should not. Here is a picture of what I want to achieve:

enter image description here

Is it possible without adding random <TextView>s? By the way, I want to do it via XML, not programmatically.

like image 996
Neurotransmitter Avatar asked Apr 11 '16 07:04

Neurotransmitter


People also ask

How do I customize my navigation drawer?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to res/layout/nav_header_main.

How do I add a header to navigation?

In the Navigation pane, click the Headings tab. Do any of the following: To move part of the document, click the heading and drag it to a new location. To change the heading's level or add a heading, right-click the heading, and then choose the action you want.


1 Answers

You are right, it's not possible to give groups a title. The only option seems to be to wrap groups into <item> and <menu> tags like this

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android">      <item android:title="General">         <menu>             <group android:checkableBehavior="single">                 <item                     android:id="@+id/nav_camera"                     android:icon="@drawable/ic_menu_camera"                     android:title="Import" />                 <item                     android:id="@+id/nav_gallery"                     android:icon="@drawable/ic_menu_gallery"                     android:title="Gallery" />             </group>         </menu>     </item>      <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> 

Resulting in a navigation drawer menu like this

Navigation Drawer

like image 59
Saenic Avatar answered Oct 21 '22 09:10

Saenic