4

How to stop the navigation drawer in my Android app from opening automatically?

It used to work fine. Out of sight at first and able to be swiped into visibility. But I needed a title for it. At first it was only a ListView. Soon after modifying the drawer xml (see below) to give it a title above a list view, it began opening automatically. I certainly didn't add anything to the code like my_nav_drawer.openOnStartup().

<!-- The navigation drawer -->
<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 main content view -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/ail_background_gradient"
    tools:context="com.allinlearning.assist_android.HomeScreenActivityFragment">

    <ImageView
        android:id="@+id/imgViewLogo"
        android:src="@drawable/ail_logo"
        android:layout_margin="10dp"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:scaleType="fitXY"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="ALL In Learning"
        android:id="@+id/textViewLogo"
        android:layout_margin="10dp"
        android:layout_below="@+id/imgViewLogo"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/font_size26"
        android:textStyle="bold" />

    <ImageButton
        android:id="@+id/imgBtnGradeAssessment"
        android:src="@drawable/grade_assessment"
        android:layout_width="100dp"
        android:layout_height="95dp"
        android:scaleType="fitXY"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/textViewGradeAssessment"
        android:layout_toStartOf="@+id/textViewGradeAssessment" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Grade"
        android:id="@+id/textViewGradeAssessment"
        android:textColor="@color/white"
        android:textSize="@dimen/font_size28"
        android:layout_centerVertical="true"
        android:layout_alignRight="@+id/imgViewLogo"
        android:layout_alignEnd="@+id/imgViewLogo" />

    <ImageButton
        android:id="@+id/imgBtnPrivateData"
        android:src="@drawable/two_clickers"
        android:layout_width="100dp"
        android:layout_height="95dp"
        android:scaleType="fitXY"
        android:layout_below="@+id/imgBtnGradeAssessment"
        android:layout_alignLeft="@+id/imgBtnGradeAssessment"
        android:layout_alignStart="@+id/imgBtnGradeAssessment" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Private Data"
        android:id="@+id/textViewPrivateData"
        android:textColor="@color/white"
        android:textSize="@dimen/font_size28"
        android:layout_alignBottom="@+id/imgBtnPrivateData"
        android:layout_toRightOf="@+id/imgBtnPrivateData"
        android:layout_toEndOf="@+id/imgBtnPrivateData"
        android:layout_marginBottom="40dp" />

</RelativeLayout>

<TextView
    android:layout_width="240dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="I AM THE TITLE"
    android:id="@+id/tvDrawerTitle"
    android:layout_margin="10dp"
    android:layout_centerHorizontal="true"
    android:textSize="@dimen/font_size26"
    android:textStyle="bold"
    android:textAlignment="center"
    android:textColor="@color/black" />

<ListView
    android:id="@+id/lvDrawerItems"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@color/white" />

Breaking news:

I've verified that in my XML above is the cause. I just reverted the navigation drawer XML back to just the ListView itself ...

<!-- The navigation drawer -->
<ListView
    android:id="@+id/lvDrawerItems"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@color/white" />

... and it no longer opens automatically. Looks and works fine. My goal is to add a TextView title without causing the drawer to open on startup.

Alyoshak
  • 2,696
  • 10
  • 43
  • 70
  • Have a look at [here](http://stackoverflow.com/a/37757349). – Mike M. Jun 10 '16 at 21:27
  • Look at the [doc](https://developer.android.com/training/implementing-navigation/nav-drawer.html#Init) about where the main and drawer should be, and **android:layout_gravity="start"** attribute. – solosodium Jun 10 '16 at 21:43
  • 1
    @Mike that was indeed the correct answer. Since you were first why don't you give a correct answer here so I can mark it for others. Thanks solosodium. – Alyoshak Jun 10 '16 at 23:00

1 Answers1

5

It's not that your drawer is opening automatically. It's that the DrawerLayout isn't finding a View to use as the drawer, so both of its direct child Views are filling it. The LinearLayout meant to be the drawer, being listed last, is covering the content View, so it just appears that the drawer is open.

DrawerLayout determines which Views to use as drawers by looking through its direct children for ones with a horizontal layout_gravity setting; i.e., left/right, or start/end. You want this attribute set on the LinearLayout, since it's now acting as the drawer. In your main layout, simply move android:layout_gravity="left" from the ListView to the LinearLayout.

Mike M.
  • 38,532
  • 8
  • 99
  • 95