Search code examples
androidandroid-layoutlistviewandroid-5.0-lollipopdrawerlayout

DrawerLayout with ListView does not process item clicks on Samsung Tab/Android 5.0.2


I'm using an android.support.v4.widget.DrawerLayout with com.android.support:appcompat-v7 in my main activity (which extends AppCompatActivity) to provide a navigation drawer, and ListView within the drawer to present user clickable items.

This all works perfectly well except on Samsung Tab devices running Android 5.0.2.

The code has been tested and works as expected on various versions of Android from 4.2.1 to 6.0.1, and works fine on an emulator running 5.0.2.

On the Samsung devices, the nav drawer is dismissed on the tap, but the new activity (e.g. MyPreferenceActivity or HelpPageActivity, in the code below) is never displayed.

My question: is there anything incorrect about the code or layout that might cause this not to work on Samsung Tab/5.0.2 devices?

The main activity is as follows:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:gravity="fill"
    android:background="@color/standard_bkgnd">

<include
    android:id="@+id/toolbar_actionbar"
    layout="@layout/toolbar_actionbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar_actionbar"
    >

    <!-- normal content view -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        ...main UI stuff...
    </LinearLayout>

    <!-- drawer view -->
    <include layout="@layout/nav_drawer" />

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

</LinearLayout>

The nav_drawer layout is as follows:

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:background="@color/button_material_dark"
    android:orientation="vertical"
    android:layout_gravity="start">

    <RelativeLayout
        android:id="@+id/drawer_header"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/material_blue_grey_800"
        android:padding="8dp" >

        <ImageView
            android:id="@+id/app_icon"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@drawable/ic_launcher_08"
            android:layout_marginTop="15dp"
            android:contentDescription="@string/app_icon"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="42dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/app_icon"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/app_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textColor="#fff"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginTop="4dp"
                android:text="@string/app_long_name"
                android:textColor="#fff"
                android:textSize="12sp" />
        </LinearLayout>
    </RelativeLayout>

    <ListView
        android:id="@+id/nav_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/drawer_header"
        android:choiceMode="singleChoice"/>

</RelativeLayout>

The ListView is configured to present a number of items that user can click to view other content within the app, or perform other activities:

// Drawer Item click listeners
    drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            switch (position) {
                case 0: // preferences
                    startActivity(new Intent(that, MyPreferenceActivity.class));
                    break;
                case 1: // help
                    startActivity(new Intent(that, HelpPageActivity.class));
                    break;
                case 2: // send feedback
                    composeEmail();
                    break;
                default:
                    break;
            }

            _drawerLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    _drawerLayout.closeDrawers();
                }
            }, 500);

            drawerList.clearChoices();

        }
    });

Any suggestions much appreciated!


Solution

  • So, it turns out not to have been anything to do with Samsung Tab/5.0.2 after all - it was coincidence that the only reports of problems I'd had came from those devices.

    The problem was that I had a landscape layout for my main activity which had the order of the normal content view and the drawer view reversed, which prevented the ListItems receiving the click event:

    <android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar_actionbar"
    >
    
    <!-- normal content view -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        ...main UI stuff...
    </LinearLayout>
    
    <!-- drawer view *** this must come after the normal content view *** -->
    <include layout="@layout/nav_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
    

    As soon as I switched them around, things worked as expected. Doh.