8

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 https://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.

0x0
  • 188
  • 2
  • 8
  • do you use a custom listview?. You can use a seelctor – Raghunandan Nov 24 '13 at 01:51
  • Nope, I'm using the built-in ListView with an ArrayAdapter. Is there a chance that it's not appearing selected because I'm using Theme.AppCompat.Light? – 0x0 Nov 24 '13 at 02:23
  • use a selector for `drawer_list_item.xml` – Raghunandan Nov 24 '13 at 05:58
  • Any luck with that issue? I am struggling with this as well. I have some kind of a workaround. I just don't use selectors and set and clear the background color by myself. Seems like a big hack and I would like to make this work with a simple selector. I am frustrated, that this costs me so much time. – Mike T Dec 09 '13 at 13:49
  • @MikeT Sadly I haven't been able to make it work with app-compat's action bar even up till now, however it works fine if I use the native action bar. I'll let you know if I get it to work. – 0x0 Dec 13 '13 at 06:00
  • 1
    Same issue here. Did you figure this one out? – Ravi Feb 28 '14 at 17:52
  • Since you are using listview for navigation item so check this link..http://stackoverflow.com/questions/2562051/listview-item-background-via-custom-selector – kundan roy Nov 05 '15 at 10:28

5 Answers5

0

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.

C B J
  • 1,838
  • 16
  • 22
0

This

navigationView.getMenu().getItem(0).setChecked(true);

will select the 1st item on the menu.

Onik
  • 19,396
  • 14
  • 68
  • 91
Thiago
  • 12,778
  • 14
  • 93
  • 110
0

use android:checked = "true" in yournavigationlist.xml file.

Eample:

<group android:checkableBehavior="single">
<item
        android:id="@+id/nav_main"
        android:checked="true"
        android:title="Home"
        />
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_menu_gallery"
        android:title="Gallery" />

</group>

item with id nav_main will be selected by default when application starts.

Imran Aslam
  • 208
  • 2
  • 15
0

Use navigationView.setCheckedItem(R.id.default) after you setup your navigationview.

hashiCode
  • 489
  • 8
  • 17
0

Add

android:checked="true"

to your specific menu item.

If you want to open specific fragment

getSupportFragmentManager().beginTransaction().replace(R.id.frame, new YourFragment()).commit();
Shiv Buyya
  • 3,770
  • 2
  • 30
  • 25