I am trying to set a left Drawer with ListView. On item click, I want to take some action but OnItemClick() doesnt get called.
Heres my code.
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mNewsCategories = getResources().getStringArray(R.array.category_arrays);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mNewsCategories));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("POSITION", "onItemClick:"+position);
mDrawerLayout.closeDrawer(mDrawerList);
}
});
}
activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer" android:layout_width="240dp"
android:layout_height="match_parent" android:layout_gravity="start"
android:choiceMode="singleChoice" android:divider="@android:color/transparent"
android:dividerHeight="0dp" android:background="#111" />
<android.support.v4.view.ViewPager
android:id="@+id/pager" android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
drawer_list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:focusable="false"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
P.S. I have looked into OnItemCLickListener not working in listview and nothing seems to be working. I tried every combination.