0

I'm trying to implement Toolbar with EditText inside that serves as a searchbox. Here's what I'm trying to achieve: enter image description here

Everything looks ok so far, but EditText behaves weirdly. When I tap on it it gains focus as expected, but when I'm tapping anywhere outside the EditText nothing happens thus neither soft keyboard is hiding nor the EditText loses focus. Here's the code for search box view:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    cardview:cardBackgroundColor="#FFFFFF"
    cardview:cardCornerRadius="2dp"
    cardview:cardElevation="4dp"
    cardview:cardUseCompatPadding="true"
    >

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#FFFFFF"
      android:descendantFocusability="beforeDescendants"
      android:focusableInTouchMode="true"
      >

    <ImageView
        android:id="@+id/search_icon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="10dp"
        android:background="@android:color/transparent"
        android:src="@drawable/ic_search_black_24dp"
        android:tint="#7d7d7d"
        />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:layout_toLeftOf="@+id/speech"
        android:layout_toRightOf="@+id/search_icon"
        android:background="@android:color/transparent"
        android:hint="Search for a store"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:textColor="#494949"
        android:textColorHint="#a9a9a9"
        android:textSize="16sp"
        />

    <ImageView
        android:id="@+id/speech"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="10dp"
        android:background="@android:color/transparent"
        android:src="@drawable/ic_mic_black_24dp"
        android:tint="#7d7d7d"
        />

  </RelativeLayout>
</android.support.v7.widget.CardView>

and here's the usage of it in the Toolbar:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

  <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        foreground="?android:windowContentOverlay"
        >

      <android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_width="match_parent"
          android:layout_height="?actionBarSize"
          app:theme="@style/Toolbar"
          app:titleTextAppearance="@style/ToolbarTitle"
          foreground="?android:windowContentOverlay"
          >
        <include layout="@layout/search_box"/>
      </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>


    <FrameLayout
        android:id="@+id/activityContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/cameraButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:src="@drawable/ic_camera_white_24dp"
        app:backgroundTint="@color/primaryGreen"
        app:fabSize="normal"
        app:pressedTranslationZ="12dp"
        />

  </FrameLayout>

  <android.support.design.widget.NavigationView
      android:id="@+id/navigationView"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:fitsSystemWindows="true"
      app:headerLayout="@layout/navigation_drawer_header"
      app:itemBackground="@drawable/drawer_item_bg"
      app:itemIconTint="@color/drawer_item"
      app:itemTextColor="@color/drawer_item"
      app:menu="@menu/navigation_menu"
      app:theme="@style/NavigationDrawer"
      />

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

Perhaps I'm missing something obvious, but I've spent last 3 hours trying to figure it out and without any luck... Did anyone have similar problem? Any help or direction will be appreciated.

lukaleli
  • 3,427
  • 3
  • 22
  • 32
  • try adding android:focusableInTouchMode="true" in edittext – Nitesh Dec 07 '16 at 20:36
  • @Nitesh it changed nothing. Actually I tried putting new EditText anywhere else in my app just for testing purposes. It doesn't lose focus as well... – lukaleli Dec 07 '16 at 20:38
  • 1
    Check this out http://stackoverflow.com/questions/4165414/how-to-hide-soft-keyboard-on-android-after-clicking-outside-edittext – BR89 Dec 07 '16 at 20:43

0 Answers0