-1

I would like to create some category, to separate the name of the items in the drawer menu. But I have only this in the strings.xml :

    <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Slider Menu</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="drawer_open">Slider Menu Opened</string>
    <string name="drawer_close">Slider Menu Closed</string>

    <!-- Nav Drawer Menu Items -->
    <string-array name="nav_drawer_items">
        <item >Home</item>
        <item >Connection</item>
        <item >Register</item>
        <item >Communities</item>
        <item >Pages</item>
        <item >Logout</item>
    </string-array>

    <!-- Nav Drawer List Item Icons -->
    <!-- Keep them in order as the titles are in -->
    <array name="nav_drawer_icons">
        <item>@drawable/ic_home</item>
        <item>@drawable/ic_people</item>
        <item>@drawable/ic_photos</item>
        <item>@drawable/ic_communities</item>
        <item>@drawable/ic_pages</item>
        <item>@drawable/ic_whats_hot</item>
    </array>

    <!-- Content Description -->
    <string name="desc_list_item_icon">Item Icon</string>

</resources>

There, it's my drawer_list_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@drawable/list_selector">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="@string/desc_list_item_icon"
        android:src="@drawable/ic_home"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/icon"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="@color/list_item_title"
        android:gravity="center_vertical"
        android:paddingRight="40dp"/>

    <TextView android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/counter_bg"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:textColor="@color/counter_text_color"/>

</RelativeLayout>

For exemple I'd like to make a separation with a small bar (or other) between "Home" and "Connection"...

zouarv42
  • 15
  • 9

2 Answers2

1

In your activity XML,

<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:menu="@menu/activity_main_drawer" />

Create activity_main_drawer.xml in res > menu,

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Slideshow" />
</group>

<item android:title="Communicate">
    <menu>
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_manage"
                android:icon="@drawable/ic_menu_manage"
                android:title="Tools" />
        </group>
    </menu>
</item>

Change the titles and icons to what you like. You can also add app:headerLayout in NavigationView to have a header on top of the menu.

Joshua
  • 5,901
  • 2
  • 32
  • 52
  • What about the listdrawer and the listview in the string ? I keep it too ? – zouarv42 Jun 20 '16 at 19:23
  • @zouarv42 `nav_drawer_items` and `nav_drawer_items` are not needed. You can refer the drawable in `andorid:icon` and string in `android:title`. – Joshua Jun 21 '16 at 01:00
  • i can't open the app now...http://www.filedropper.com/thyroid I have a navigation and a drawer layout in the main.xml --> not good ? – zouarv42 Jun 21 '16 at 07:35
  • @zouarv42 I took a look at the code. You cannot have two drawers on the left. You are having the `ListView` and `NavigationView` as drawers. You need to remove the `ListView` in `activity_main.xml` and modify the code in `MainActivity`. – Joshua Jun 21 '16 at 08:10
  • I think I have modified with the correct way the main.xml but still problems in the NavDrawerListAdapter.class : ```` 'ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon); TextView txtTitle = (TextView) convertView.findViewById(R.id.title); TextView txtCount = (TextView) convertView.findViewById(R.id.counter);'```` – zouarv42 Jun 21 '16 at 08:45
  • @zouarv42 It is because those IDs declaration are inside the `ListView`. When you remove the `ListView`, those IDs no longer exists. – Joshua Jun 21 '16 at 08:47
  • I've done some cleaning about the Listview, no errors, the app lauching but then black screen :) I've upload the code here again: http://www.filedropper.com/thyroid_1 Thx for your time – zouarv42 Jun 21 '16 at 09:19
  • For some reasons, the link keep giving me empty file. – Joshua Jun 21 '16 at 10:54
  • http://www.filedropper.com/works – zouarv42 Jun 21 '16 at 11:07
  • It works on my side. http://imgur.com/a/aUEby Although I changed the gradle build tool to 24.0.0 because I do not install rc2, and I also updated the target platform and related library to 24. You may give it a try. – Joshua Jun 21 '16 at 11:31
  • Yeah of course but the list in the drawer menu is still the same even with the new navigation drawer and no more Listview... – zouarv42 Jun 21 '16 at 11:40
  • Should I link it from the string.xml ? – zouarv42 Jun 21 '16 at 18:59
  • Soory to bother you, I didn't find any soluitons...:( – zouarv42 Jun 25 '16 at 12:48
0

Use a menu resource (with groups) instead of a string-array, as suggested here.

Community
  • 1
  • 1
Bryan
  • 14,756
  • 10
  • 70
  • 125
  • Thx for your response but should I keep the List ? How could I do it cause I've my main and just the menu---> activity_main_drawer (new), that's mean I should make a choice... – zouarv42 Jun 20 '16 at 19:45