Android-Studio, EditText inside RecyclerView inside CustomDialog focus issue

Issue

I have a problem with the focus. Everytime I click in the EditText I can write 1 character and then it loses the focus.

I´ve searched and I found some solutions if my EditText is inside a RecyclerView, but those don´t work with my project. I suspect that this is because my EditText is inside a RecyclerView which is inside a CustomDialog.

If someone know how to solve this it would be great 😀

Edit:

This is the xml code of the CustomDialog that contains the RecyclerView:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:foregroundTint="#009688">

    <androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <EditText
                android:id="@+id/nombreGasto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Nombre"
                android:inputType="textPersonName"
                android:minHeight="48dp" />

            <EditText
                android:id="@+id/precioGasto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="Precio"
                android:inputType="numberDecimal"
                android:minHeight="48dp" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerGasto"
                android:layout_width="match_parent"
                android:layout_height="145dp" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="10dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/cancelButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:backgroundTint="#FFFFFF"
                    android:text="Cancel"
                    android:textColor="#00BCD4" />

                <Button
                    android:id="@+id/addButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:backgroundTint="#FFFFFF"
                    android:text="OK"
                    android:textColor="#00BCD4" />
            </LinearLayout>
        </LinearLayout>

    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

And this is the xml code of the CardView, which is the View that receives the RecyclerView, that contains the EditText:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/linearLay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/personNamePrecio"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="PersonaX"
                android:textAlignment="viewStart"
                android:textSize="24sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/editTextPrecioPersona"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:hint="0.00"
                android:inputType="numberDecimal"
                android:minHeight="48dp" />

        </LinearLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

We put requestFocus() and requestFocusFromTouch() and it works:

#personGasto is the editText
override fun afterTextChanged(p0: Editable?) {
                    #...
                    #some code
                    #...
                    personGasto.requestFocus()
                    personGasto.requestFocusFromTouch()
}

Answered By – Adrián Mon Maroto

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published