1

I have a working DrawerLayout with several elements within it. All elements inside the drawer are located within a RelativeLayout. The weird part is whenever the drawer is opened, the EditText receives focus automatically, and never looses it. I tried adding android:clickable="true" attribute to the parent element (which is the RelativeLayout) to trick the focus issue but it does not help. Any ideas what is causing this behavior?

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

    <!-- The first child in the layout is for the main Activity UI-->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        android:background="#ffffffff">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listViewChat"
            android:layout_centerHorizontal="true"
            android:choiceMode="multipleChoice" />

    </RelativeLayout>

    <!-- Side navigation drawer UI -->
    <RelativeLayout
        android:id="@+id/drawerContainer"
        android:layout_width="270dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="@drawable/splash_background_blur"
        android:clickable="true">

        <!--this custom view serves as a dimmer for the drawer -->
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#73000000"
            android:id="@+id/view1"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <!--this edit text receives focus automatically on drawer open -->
        <!--and never looses focus as long as the drawer is opened -->
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editTextUserName"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="160dp"
            android:cursorVisible="false"
            android:textColor="#FFF"
            android:textSize="20sp"
            android:background="@null"
            android:maxLength="15"
            android:inputType="textNoSuggestions"/>

        <Button
            android:layout_width="220dp"
            android:layout_height="35dp"
            android:text="@string/buttonSelectPhoto"
            android:id="@+id/buttonTakePhoto"
            android:background="@drawable/splash_button"
            android:textColor="#FFF"
            android:layout_marginTop="200dp"
            android:layout_marginLeft="25dp"
            android:layout_marginStart="25dp"/>


        <ListView
             android:id="@+id/navList"
             android:layout_width="match_parent"
             android:layout_height="match_parent"/>

    </RelativeLayout>

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


EDIT : Just to sharpen things a bit: I test the app on the emulator, and when I reveal the drawer without touching a thing, the editText gains focus (meaning I swipe to the right, drawer is visible, I type on the keyboard and the text is seen inside the editText without actually clicking it)

Alex
  • 1,982
  • 4
  • 37
  • 70
  • see these links: http://stackoverflow.com/questions/5056734/android-force-edittext-to-remove-focus http://stackoverflow.com/questions/4828636/edittext-clear-focus-on-touch-outside – Umair Apr 08 '15 at 14:04

3 Answers3

0

Use

android:windowSoftInputMode="stateAlwaysHidden" in the activity tag of the Manifest.xml

Fahim
  • 12,198
  • 5
  • 39
  • 57
  • Please not, I updated the description of my problem. I tried what you suggested but it didnt help. – Alex Apr 08 '15 at 14:13
0

I figured out what was my problem. The last ListView was causing the problems. I removed it and now the drawer behaves as intended.

Alex
  • 1,982
  • 4
  • 37
  • 70
0

Add following line in Parent layout of edit text which is relative layout in your case

 android:descendantFocusability="afterDescendants"

and

 android:focusableInTouchMode="true"
Jaykishan Sewak
  • 822
  • 6
  • 13