2

I have stopped programming Android in Eclipse and I start to develop in Android Studio. I have a app in Eclipse with de following navigation drawer:

enter image description here

The menu item have a number that indicates how many items inside. I in Eclipse do this by listview adapter but in Android Studio I have a menu.xml that it have the item information.

 <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="Resolución OS" />

How I put a number and how I can update it from Java code?

Thanks and sorry for my english, it is very very bad.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • You checke [this link](http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/). Instead of menu use custom list item design and use ListView / RecyclerView. Check [this too](http://stackoverflow.com/a/31716815/1728206) – janani Jan 22 '16 at 12:50

3 Answers3

0

When you define a DrawerLayout, it works like this

<android.support.v4.widget.DrawerLayout
    android:id="@+id/root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- VIEWGROUP FOR MAIN CONTENT -->
    <!-- this is what you see by default -->

    <!-- VIEWGROUP FOR LEFT DRAWER -->
    <!-- set Layout_Gravity as START (or LEFT) -->

    <!-- VIEWGROUP FOR RIGHT DRAWER -->
    <!-- set Layout_Gravity as END (or RIGHT) -->
</android.support.v4.widget.DrawerLayout>

So if you set a view group as the left drawer which contains those elements in that form, then it'll look just like that. Basically you need either a recycler view or just specify them manually (or create your own custom viewgroup).

<RelativeLayout  android:id="@+id/left_drawer_clickable_item"
                 android:layout_width="match_parent" 
                 android:layout_height="24dp">
    <LinearLayout android:id="@+id/left_drawer_icon_holder" 
                  android:layout_width="24dp" 
                  android:layout_height="24dp" 
                  android:orientation="vertical"
                  android:layout_alignParentLeft="true">
        <ImageView android:id="@+id/left_drawer_icon" 
                   android:layout_width="match_parent" 
                   android:layout_height="match_parent" 
                   android:src="@drawable/icon"/>
    </LinearLayout>
    <TextView android:layout_width="wrap_content" 
              android:layout_height="match_parent"
              android:id="@+id/left_drawer_number"
              android:layout_alignParentRight="true"/>
    <TextView android:id="@+id/left_drawer_text"
              android:layout_width="match_parent" 
              android:layout_height="match_parent"
              android:layout_toLeftOf="@+id/left_drawer_number"
              android:layout_toRightOf="@+id/left_drawer_icon"/>
</RelativeLayout>

If you use that in a RecyclerView as the view parameter of a View Holder, you can parametrize it accordingly (unless you actually want to go with NavigationView instead in the Design Support Library).

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

Create counter layout, i.e. menu_counter.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:textAppearance="@style/TextAppearance.AppCompat.Body2" />

Reference it in your drawer menu xml, i.e. menu/drawer.xml:

<item
    ...
    app:actionLayout="@layout/menu_counter" />

Remember to use app instead of android namespace.

0

Try to use liatview or recycler view to create left drawer . Write your custom adapter inside which you can easy update whatever you want . You can simply change the data passed to listview or recycler view through adapter and see it updated.