Android – How to make a specific item in the navigation drawer to appear as selected

androidandroid-fragmentslistviewnavigation-drawer

I have been frustrated with this problem for quite a while, but I can't seem to make an item in the navigation drawer to appear as selected neither programmatically nor when the user selects it.

I am trying to achieve the following
http://i.stack.imgur.com/PIHEm.png (I cannot post images yet)

I have followed the example navigation drawer at http://developer.android.com/training/implementing-navigation/nav-drawer.html
and whenever I switch fragments, I call setItemChecked() on the navigation drawer's ListView

mDrawerListView.setItemChecked(position, true);, but it does not seem to select the item at the position in the listview.

My layout file is also almost exactly like Google's example

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

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView 
        android:id="@+id/left_drawer"
        android:layout_width="160dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#e5e5e5" />

</android.support.v4.widget.DrawerLayout>

Any help is appreciated greatly, and thanks in advance.

EDIT: Here is my initialization code for the nav-drawer. As you can see, I am not using a custom ListView or Adapter.

 private void setUpDrawer() {
        mDrawerListViewItems = getResources().getStringArray(R.array.drawer_list_item_names);
        mDrawerListView = (ListView) findViewById(R.id.left_drawer);
        mDrawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mDrawerListViewItems));

        mDrawerListView.setOnItemClickListener(new ListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectCounterFragment(position);
            }
        });

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                );

        // Set actionBarDrawerToggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        // styling option add shadow the right edge of the drawer
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    }

Thank you all, for the responses so far.

Best Answer

What is the layout for your list items? There are a number of traps with list item layouts "swallowing" state resulting in the listview not being selectable/clickable.

I would recommend taking a look at the excellent articles by Cyril Mottier, look especially at the section "Why, the hell, are my itemviews no longer clickable?"

Good luck,

CJ.