1

Actually i'm trying to make an app for devices with or without physical keyboard.

I've added a setOnEditorActionListener on my first editText and when actionDone is pressed or Enter button is pressed on a physical keyboard it should make visible the other editText and focus on it but it doesn't focusing on it.

Here is my OnEditorActionListner method:

  public void codeText(){
        code.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if(i == EditorInfo.IME_ACTION_DONE || keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER){
                    if(!code.getText().toString().equals("")){
                        if (TextUtils.isDigitsOnly(code.getText())) {
                            if (code.getText().length() >= 1 && code.getText().length() <= 999999) {
                                qtaBox.setVisibility(View.VISIBLE);
                                qtaText.setVisibility(View.VISIBLE);
                                qta.requestFocus();
                            }
                        }
                    }
                }
                return true;
            }
        });
    }

Full xml code, the editText from which i'm trying to focus is barcodeTxt the edittext i'm trying to focus is qtaTxt

     <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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#ffffff"
    tools:context=".SettingsActivity">

    <android.support.v7.widget.CardView
        android:id="@+id/logo"
        android:layout_margin="2dp"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_alignParentTop="true"
        app:cardBackgroundColor="#001F54"
        app:cardCornerRadius="5dp"
        android:layout_centerHorizontal="true"
        app:cardElevation="4dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            tools:ignore="UseCompoundDrawables">

            <ImageButton
                android:layout_width="0dp"
                android:layout_weight="0.2"
                android:layout_height="60dp"
                android:id="@+id/backButton"
                android:layout_gravity="center"
                android:scaleType="fitCenter"
                android:src="@drawable/arrowback"
                android:background="#00454545"
                android:padding="8dp"
                tools:ignore="ContentDescription" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginEnd="10dp"
                android:gravity="center_vertical"
                android:textAlignment="textEnd"
                android:text="INVENTARIO"
                android:textColor="#ffffff"
                android:textSize="20sp"
                android:textStyle="bold"
                tools:ignore="HardcodedText,RtlCompat" />

            <ImageView
                android:layout_marginEnd="10dp"
                android:id="@+id/imageView"
                android:layout_width="0dp"
                android:layout_weight="0.2"
                android:layout_height="match_parent"
                android:layout_gravity="end"
                android:padding="5dp"
                android:src="@drawable/storage"
                tools:ignore="ContentDescription" />

        </LinearLayout>


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


    <LinearLayout
        android:animateLayoutChanges="true"
        android:layout_below="@id/logo"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:background="#001f54"
        android:orientation="vertical">


<TextView
    android:id="@+id/barcodeText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="12sp"
    android:layout_marginStart="3dp"
    android:layout_marginBottom="2dp"
    android:text="Barcode:"
    android:textColor="#b1b1b1"
    tools:ignore="HardcodedText" />

    <android.support.v7.widget.CardView
        android:id="@+id/barcodeBox"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        app:cardBackgroundColor="#01358f"
        app:cardCornerRadius="5dp"
        app:cardElevation="4dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            tools:ignore="UselessLeaf">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="match_parent"
                android:layout_gravity="end"
                android:layout_marginStart="4dp"
                android:padding="5dp"
                android:src="@drawable/barcodesticker"
                android:tint="#ffffff"
                tools:ignore="ContentDescription" />


            <EditText
                android:id="@+id/barcodeTxt"
                android:selectAllOnFocus="true"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:imeOptions="actionDone"
                android:inputType="number"
                android:focusable="true"
                tools:ignore="Autofill,LabelFor" />


        </LinearLayout>


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

        <TextView
            android:id="@+id/textQuantity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:layout_marginTop="2dp"
            android:layout_marginStart="3dp"
            android:layout_marginBottom="2dp"
            android:text="Quantità:"
            android:textColor="#b1b1b1"
            tools:ignore="HardcodedText" />

    <android.support.v7.widget.CardView
        android:id="@+id/qtaBox"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        app:cardBackgroundColor="#a78824"
        app:cardCornerRadius="5dp"
        app:cardElevation="4dp">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            tools:ignore="UselessLeaf">

            <TextView
                android:layout_marginStart="4dp"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="QTA"
                android:padding="5dp"
                android:textColor="#ffffff"
                android:textStyle="bold"
                android:gravity="center"
                android:textAlignment="center"
                tools:ignore="HardcodedText" />


            <EditText
                android:id="@+id/qtaTxt"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:imeOptions="actionDone"
                android:maxLength="4"
                android:selectAllOnFocus="true"
                android:inputType="number"
                android:focusable="true"
                tools:ignore="Autofill,LabelFor" />


        </LinearLayout>



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

        <TextView
            android:id="@+id/textLast"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:layout_marginTop="2dp"
            android:layout_marginStart="3dp"
            android:layout_marginBottom="2dp"
            android:text="Ultimo articolo letto:"
            android:textColor="#b1b1b1"
            tools:ignore="HardcodedText" />

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:id="@+id/last"
            app:cardBackgroundColor="#c800544e"
            app:cardCornerRadius="5dp"
            app:cardElevation="0dp">



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                tools:ignore="UselessLeaf">


                <ImageView
                    android:layout_marginStart="4dp"
                    android:layout_width="40dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="end"
                    android:padding="5dp"
                    android:src="@drawable/barcodesticker"
                    android:tint="#ffffff"
                    tools:ignore="ContentDescription" />

                <TextView
                    android:id="@+id/lastBarCode"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                    android:text=""
                    android:padding="5dp"
                    android:textColor="#ffffff"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:textAlignment="center"
                    tools:ignore="HardcodedText" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="QTA"
                    android:padding="5dp"
                    android:textColor="#ffffff"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:textAlignment="center"
                    tools:ignore="HardcodedText" />

                <TextView
                    android:id="@+id/lastQta"
                    android:layout_width="0dp"
                    android:layout_weight="0.3"
                    android:layout_height="match_parent"
                    android:text=""
                    android:padding="5dp"
                    android:textColor="#ffffff"
                    android:textStyle="bold"
                    android:gravity="center"
                    android:textAlignment="center"
                    tools:ignore="HardcodedText" />

            </LinearLayout>


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


        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            app:cardBackgroundColor="#e3008f43"
            app:cardCornerRadius="5dp"
            app:cardElevation="0dp">

            <Button
                android:id="@+id/btnConferma"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="CONFERMA"
                android:background="#00001f54"
                tools:ignore="HardcodedText" />

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

    </LinearLayout>






    <android.support.v7.widget.CardView
        android:id="@+id/buttonBox"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_margin="2dp"
        app:cardBackgroundColor="#001F54"
        app:cardCornerRadius="5dp"
        app:cardElevation="4dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            tools:ignore="UseCompoundDrawables">

            <ImageButton
                android:id="@+id/itemList"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="0.2"
                android:background="#00454545"
                android:padding="8dp"
                android:scaleType="fitCenter"
                android:src="@drawable/history"
                tools:ignore="ContentDescription" />

            <View
                android:id="@+id/divider"
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="3dp"
                android:layout_marginTop="3dp"
                android:background="@drawable/gradient_separator"
                tools:ignore="DuplicateIds,InefficientWeight,NestedWeights" />

            <ImageButton
                android:id="@+id/sendBtn"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_gravity="center"
                android:layout_weight="0.2"
                android:background="#00454545"
                android:padding="8dp"
                android:scaleType="fitCenter"
                android:src="@drawable/send"
                tools:ignore="ContentDescription" />


        </LinearLayout>


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

</RelativeLayout>
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100

1 Answers1

0

You seem to be requesting focus to the qta reference instead of your EditText element. So maybe first try to do qtaText.requestFocus().

If that didn't fix it, there might be something wrong with your XML config. While not being an expert on the topic, I do have a focusable EditText element in my latest project, where the XML looks like this:

<EditText
    android:id="@+id/email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="@font/opensans_light"
    android:hint="@string/prompt_email"
    android:inputType="textEmailAddress"
    android:keepScreenOn="true"
    android:maxLines="1"
    android:singleLine="true"
    android:textColor="@android:color/white" />

Hope this helps you solve the issue.

Matteus
  • 97
  • 2
  • 11