Home » Android » Android: EditText NextFocusDown not trigger Spinner

Android: EditText NextFocusDown not trigger Spinner

Posted by: admin June 15, 2020 Leave a comment

Questions:

In my application I have one registration screen, There are lot of EditTexts and Spinners.
I want to go through the Registrations field one by one.

So I applied android:imeOptions="actionNext" . But It ignores all the Spinners. It will give focus only to EditText. I have also tried setNextFocusDownId(). This is also ignore the spinners.

   <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/reportentry11"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/numbers"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:imeOptions="actionNext"
            android:inputType="phone" 
            >
           <requestFocus/>
        </EditText>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reportentry12"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            <TextView
                android:id="@+id/txt_exp"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"

                android:text="EXP:"
                android:textColor="#000000"
                android:textSize="12dp" />

            <Spinner
                android:id="@+id/spin_date"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="3"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:nextFocusDown="@+id/spin_year"
                android:text="date"
                android:textSize="12dp" />

            <Spinner
                android:id="@+id/spin_year"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginRight="5dp"

                  android:nextFocusDown="@+id/cvc"
                android:text="year"
                android:textSize="12dp" />
        </LinearLayout>

        <EditText
            android:id="@+id/cvc"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:hint="@string/reg_label_cvc"
            android:imeOptions="actionNext"
            android:inputType="phone" />

        <EditText
            android:id="@+id/fname"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/reg_label_fname"
            android:imeOptions="actionNext" />

        <EditText
            android:id="@+id/address"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/reg_label_address"
            android:imeOptions="actionNext" />

        <EditText
            android:id="@+id/city"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/reg_label_city"
            android:imeOptions="actionNext"
            android:nextFocusDown="@+id/pr_spin" />

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reportentry13"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/txt_pr"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="PROV:"
                android:textColor="#000000"
                android:textSize="12dp" />

            <Spinner
                android:id="@+id/pr_spin"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="date"
                android:imeOptions="actionNext"
                android:textSize="14dp" />

            <EditText
                android:id="@+id/pcode"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:hint="@string/reg_label_pcode"
                android:imeOptions="actionDone" />
        </LinearLayout>

        <Button
            android:id="@+id/register_register_button"
            android:layout_width="wrap_content"
            android:background="@drawable/green_button_bg"
            android:onClick="completeClicked"
            android:text="@string/reg_label_complete"
            android:textSize="28dp"
            android:textStyle="bold" />
    </LinearLayout>

Please provide me the best way to trigger the spinners.

How to&Answers:

On your edit texts, override onEditorAction and give focus (or do whatever like open your spinner)…

yourEditTXT.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView view, int actionID, KeyEvent event) {
            if (actionID == EditorInfo.IME_ACTION_NEXT) {
                //do your stuff here...
                return true;
            }
            return false; 
    }
});     

Edit 12/4:
I see you were still struggling with this as of last night, if you didn’t find a solution (and didn’t post one) or to help others who may read this, this may help to get from spinner to edit text.

mySpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());            

public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parentview, View v, int position, long id) {
            // your spinner proces code 
            // then when you are done, 
            yourEditText.setFocusableInTouchMode(true);  //if this is not already set
            yourEditText.requestFocus();  //to move the cursor
            final InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);  // this line and the one above will open the soft keyboard if it doesn't already open
        }

        public void onNothingSelected(AdapterView<?> arg0) { }
    };

Answer:

do the following in your xml file:

<Spinnner
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:nextFocusDown="@+id/youedittextid"
 />

Answer:

Apart from the solutions above to focus on Spinners, here are further troubles with ime options:

If the spinners are between the EditTexts, then the EditTexts that are below the spinners ignore imeOption="actionDone" and lines="1" attributes. In both the cases they accept enter keys and move to next line, when, it should not allow the enter key event.

I found that though the listener for ime was still intact, the ime option set to whatever (next or done), it changed to 0.

So my answer is for second part while I am still trying to make spinners focusable:

xml:

     <EditText
                android:id="@+id/et_1"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_40"
                android:layout_marginLeft="@dimen/dp_8"
                android:layout_marginStart="@dimen/dp_8"
                android:gravity="start|bottom"
                android:hint="@string/et_1"
                android:imeOptions="actionNext"
                android:inputType="textCapWords" />
<!--This ime option works fine-->
<!--lots of spinners in between these Edit texts-->

                <EditText
                android:id="@+id/et_2"
                android:layout_gravity="start|bottom"
                android:layout_marginLeft="@dimen/dp_8"
                android:layout_marginStart="@dimen/dp_8"
                android:gravity="start|bottom"
                android:hint="@string/et_2"
                android:imeOptions="actionDone" />
<!--this actionDone is ignored and set to 0-->
[value of actionNext = 5 while that of actionDone = 0, but for edit text et2 reports as 0 in the listener] set listener:

...
        EditText et_1 = view.findViewById(R.id.et_1);
        fet_1.setOnEditorActionListener(this);
        //==
        EditText et_2 = view.findViewById(R.id.et_2);
        et_2.setOnEditorActionListener(this);
...

The handling of listeners where the ime actions are ignored too:

@Override
    public boolean onEditorAction(TextView textView, int actionID, KeyEvent keyEvent) {
        FontEditText et_1 = getView().findViewById(R.id.et_1);
        //==
        EditText et_2 = getView().findViewById(R.id.et_2);
        final int id = textView.getId();
        switch (id) {
            case R.id.et_1: {
                if (actionID == EditorInfo.IME_ACTION_NEXT) {
                    // works actionID = 5.
//inherited method
                    activity.hideKeyboard(textView);
                    fet_bus_entity.clearFocus();
                    et_business_add.requestFocus();
                    et_business_add.performClick();
                    return true;
                }
            }
            break;
            case R.id.et_2: {
//following line doesn't work hence commented, value of actionID = 0 here.
//                if (actionID == EditorInfo.IME_ACTION_DONE) {

                    et_business_add.clearFocus();
//inherited method
                    activity.hideKeyboard(textView);
                    return true;
//                }

            }
//            break;
        }
        return false;
    }