0

While doing a drawer styling spike for a project, I created the following listener inner class for my drawer:

 private class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        switch(position) {

            case 0:
                Toast.makeText(MainActivity.this, "Item 1 clicked", Toast.LENGTH_SHORT)
                        .show();
                Log.i(TAG, "Item 1 clicked.");

                //Highlight the selected item
                mDrawerList.setItemChecked(position, true);
                //And close the drawer on click
                mDrawerLayout.closeDrawer(mDrawerList);
            break;

            case 1:
            (...)

Which is assigned further up in the class:

 // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mItems));

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

Strangely this doesn't work. The list items never change colour. I even tried not calling closeDrawer() and checking if the colour changed on the items. The Drawer bit from MainActivity layout xml seems fine (I tried with and without listSelector):

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="320dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@android:color/white"
    android:listSelector="@drawable/abc_list_selector_holo_light"/>

And so does my custom list item:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@android:dimen/app_icon_size"
    android:textSize="@dimen/abc_text_size_large_material">
</TextView>

This was all following the guidelines and tutorials for a navigation drawer from the Android developer website: Creating a Navigation Drawer | Android Developers

Deegriz
  • 1,935
  • 1
  • 18
  • 31

2 Answers2

1

Use selector as background of textview

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@android:dimen/app_icon_size"
        android:textSize="@dimen/abc_text_size_large_material"
 android:background="@drawable/abc_list_selector_holo_light" >
    </TextView> 
Umesh Chhabra
  • 268
  • 1
  • 8
  • EDIT: Well, actually it still doesn't work with a custom list layout – Deegriz Mar 22 '15 at 15:38
  • Use selector as background of root layout of custom list item – Umesh Chhabra Mar 22 '15 at 15:40
  • I've done that by refering to this: http://stackoverflow.com/questions/23895941/changing-navigation-drawer-selected-item-color-from-default-blue?answertab=votes#tab-top question, but still no luck – Deegriz Mar 22 '15 at 15:59
1

I will post code of this layout, change according to your need. enter image description here

Umesh Chhabra
  • 268
  • 1
  • 8
  • There's no need I managed to get it to work. The only thing that's not working is the code – Deegriz Mar 22 '15 at 16:08
  • Turns out although a lot of answers said the attribute listSelector wasn't needed, I actually needed to refer to my custom listSelector from my ListView. Thank you for your help! – Deegriz Mar 22 '15 at 16:12