Using XML from an Actionbar to create a Navigation Drawer menu

Asked

Viewed 379 times

0

Good morning Everybody, My first post here.

I’m with an Android project almost ready, missing just put the icons in the Menu Navigation Drawer.

I used the following tutorial to put the items in the menu.

https://thalesondev.wordpress.com/2014/09/12/utilizando-xml-para-construir-um-menu-navigation-drawer/

My code is as follows.

List<MenuItem> menuItems = createMenu();
    adapter = new ArrayAdapter<MenuItem>(
                    getActionBar().getThemedContext(),
                    R.layout.drawer_item,
                    R.id.title3,
                    menuItems);

    mDrawerListView.setAdapter(adapter);

In the tutorial it says the following: If we want an icon on each item in the menu list, just implement an Adapter with a layout that contains an image and use the icon property of the menuitem itself to specify the image of the menu item.

The above layout already contains the image.

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

<ImageView
    android:id="@+id/icones"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="5dp"
    android:background="@drawable/ic_medi"
     />

<TextView
    android:id="@+id/title3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/icones"
    android:layout_toEndOf="@+id/icones"
    android:layout_toRightOf="@+id/icones"
    android:gravity="center_vertical"
    android:text="Line 1"
    android:textColor="#000"
    android:textSize="15sp" />

Only that the image in question keeps repeating itself not and altered in the same way as the text is.

The menu is as follows :

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

<item
    android:id="@+id/menu_home"
    android:icon="@drawable/ic_medi"
    android:title="@string/lblleitura"
    android:showAsAction="always" />

<item
    android:id="@+id/menu_grafic_valores"
    android:icon="@drawable/ic_graf"
    android:title="@string/lblGraficoValor"
    android:showAsAction="always"/>

<item
    android:id="@+id/menu_grafic_consumo"
    android:icon="@drawable/ic_graf"
    android:title="@string/lblGraficoConsumo"
    android:showAsAction="always"/>

  <item
    android:id="@+id/menu_grafic_consumo_medio"
    android:icon="@drawable/ic_graf"
    android:title="@string/lblGraficoMedio"
    android:showAsAction="always"/>

    <item
    android:id="@+id/menu_noti"
    android:icon="@drawable/ic_noti"
    android:title="Notícias"
    android:showAsAction="always"/>

     <item
    android:id="@+id/menu_sair"
    android:icon="@drawable/ic_sair"
    android:title="Sair"
    android:showAsAction="always"/>

Just this detail is missing to finish the project. I thank you for your help.

  • 1

    it’s been a while since I’ve looked at android but the fact that you already set a :background and then in the items set a :icon not conflict? try to take :background from imageView or exchange the item :icon for :background

  • Good morning Lucas, I made your suggestion and unfortunately it didn’t work. Removing the background of the imageView does not appear any image in the listview. And tbm does not have the background property for menu items. My problem I believe is in the Adapter because I am not passing the field in which the menu icons would have to be placed, but if I put there the system already shows error. I would have to change the Menuview Adapter.

  • http://hmkcode.com/android-custom-listview-titles-icons-counter/ see this link might help you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.