Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Drawer highlight selected item not working

I'm trying to highlight the selected nav drawer item but it doesn't work. it only highlights on pressing the items but does not remain highlighted after the item is selected.

I have the following code:

The ListView:

<ListView
    android:id="@+id/drawer_listview"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:choiceMode="singleChoice"
    android:divider="@color/drawer_divider"
    android:dividerHeight="@dimen/drawer_divider_height"
    android:listSelector="@drawable/list_selector_holo_light" />

The Selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" android:state_window_focused="false"/>
<item android:drawable="@drawable/list_selector_disabled_holo_light" android:state_enabled="false" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/list_selector_disabled_holo_light" android:state_enabled="false" android:state_focused="true"/>
<item android:drawable="@drawable/list_selector_background_transition_holo_light" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/list_selector_background_transition_holo_light" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@drawable/list_activated_holo" android:state_activated="true" />
<item android:drawable="@drawable/list_focused_holo" android:state_focused="true"/>

The drawables are 9-patch files generated with Android Holo Colors.

In my activity:

  mListView.setAdapter(mAdapter);
  mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  mListView.setItemChecked(1, true); // Testing
  mListView.setSelection(1); // Testing

As far as I know, the state_activated="true" in the selector is when the listView item is checked/selected. but it doesn't work.

Edit:

I set android:background="@drawable/list_selector_holo_light" for the row layout and now it is working, but I still have no idea why listSelector is not working.

like image 538
Nima G Avatar asked Jul 05 '14 12:07

Nima G


Video Answer


1 Answers

Which version of Android are you using?

I think state_activated is working for API level 11 and higher.

I have experienced this and in order to handle Pre Honeycomb, I create a customized Adapter for ListView and have following code in getView method:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
     if (mListView.isItemChecked(position)) {
           holder.tvDrawerItem.setBackgroundColor(R.drawable.list_activated_holo);
     } else {
           holder.tvDrawerItem.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
     }
}

Addenda: support for Pre HoneyComb using android support Library v4.

If you suppose to support Android 4+, just check Android developer Sample : http://developer.android.com/training/implementing-navigation/nav-drawer.html and check drawer_list_layout. activatedBackgroundIndicator is what you need:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
like image 132
Ali Avatar answered Sep 17 '22 20:09

Ali