I have an extension of EditText that i call on my onCreate activities to hide keyboard when edittext looses the focus:
fun EditText.hideSoftKeyboardOnFocusLostEnabled(enabled: Boolean) {
val listener = if (enabled)
OnFocusLostListener()
else
null
onFocusChangeListener = listener
}
So on my onCreate i call:
myEditText!!.hideSoftKeyboardOnFocusLostEnabled(true)
The problem is that now i have an activity where the edittext is inside a NestedScrollView and this don't work:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.liderasoluciones.smt.presentation.enviarmultimedia.EnviarMultimediaActivity"
android:clickable="true"
android:focusableInTouchMode="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/coordinatorLayout">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/nestedScrollView">
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.liderasoluciones.smt.presentation.enviarmultimedia.EnviarMultimediaActivity">
<TextView android:id="@+id/enviarMultimediaLblEmpleado"
android:text="Empleado"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner android:id="@+id/enviarMultimediaListaEmpleados"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/enviarMultimediaLblMatricula"
android:text="@string/LICENSE_PLATE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/enviarMultimediaActivityMatricula"
android:inputType="text"
android:hint="@string/EX_LICENSE"/>
<TextView android:id="@+id/lblOT"
android:text="OT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Spinner android:id="@+id/listaOTS"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/enviarMultimediaActivityImagenes"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="@string/enviar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/enviarMultimediaActivityBtnEnviar"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</android.support.constraint.ConstraintLayout>
I added the NestedScrollView due to the RecyclerView where i have inside a cardview. Without the nestedscrollview, the cardview uses the rest of the device available heigh (with a scroll) and later the button doesn't appear. Witht he nested scrollview the view works as a want (works like a unique scroll for the whole window), but breaks me the option to hide the keyboard when the user changed the focus form the edittext to other element.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="16dp"
android:layout_margin="8dp"
app:cardElevation="8dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="@+id/contentEnviarMultimediaConstraintLayout"
>
<ImageView
android:id="@+id/contentEnviarMultimediaImagen"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:contentDescription="Imagen a enviar"
app:srcCompat="@drawable/android_logo" android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="4dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginEnd="4dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginBottom="4dp"
app:layout_constraintBottom_toBottomOf="parent"/>
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchorGravity="left|center"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/contentEnviarMultimediaConstraintLayout"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="@+id/contentEnviarMultimediaConstraintLayout"
android:id="@+id/contentEnviarMultimediaProgressBar"/>
<ImageView
android:id="@+id/contentEnviarMultimediaEliminarImagen"
android:layout_width="25dp"
android:layout_height="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="Eliminar" app:srcCompat="@drawable/eliminar"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
So is there a way to maintain the nestedscrollview and still detect the lost focus of the edittext.