'Next focus on custom view inside RecyclerView

I have RecyclerView that represent forms. These RecyclerView are made of multiple type of View. Each of them has a base_layout and specific View depending on the data-type they represent (eg: a string will be represented by an EditText).

Here is the layout file representing a string data-type:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include android:id="@+id/base_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/view_form_field_base"
        app:label="@{viewModel.label}"
        app:errorMessage="@{viewModel.formElement.errorMessage}"
        app:canGenerate="@{viewModel.formElement.canBeGenerated &amp;&amp; viewModel.editable}" />

    <com.my.app.views.edittext.ActionEditText
        android:id="@+id/field_content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        android:layout_marginEnd="8dp"
        android:padding="@{viewModel.editable ? 10 : 0}"

        android:background="@{viewModel.editable ? @drawable/my_edit_text_background : @drawable/empty_drawable}"

        android:clickable="@{viewModel.editable}"
        android:cursorVisible="@{viewModel.editable}"
        android:focusableInTouchMode="@{viewModel.editable}"
        android:focusable="@{viewModel.editable}"
        android:inputType="textMultiLine|textNoSuggestions"

        android:ellipsize="end"
        android:text="@={viewModel.formElement.value}"
        app:layout_constraintBottom_toTopOf="@+id/error_message"
        app:layout_constraintEnd_toStartOf="@+id/generate_button"
        app:layout_constraintStart_toStartOf="@+id/label"
        app:layout_constraintTop_toBottomOf="@+id/label" />

</android.support.constraint.ConstraintLayout>

When a form is simply made of strings, their no issues. But their is also some custom views. For example:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include android:id="@+id/base_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/view_form_field_base"
        app:label="@{viewModel.label}"
        app:errorMessage="@{viewModel.formElement.errorMessage}"/>

    <com.my.app.views.dynamic_list.DynamicRadioGroup
        android:id="@+id/field_content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="@+id/label"
        app:layout_constraintEnd_toStartOf="@+id/medical_record_level"
        app:layout_constraintBottom_toTopOf="@+id/error_message"
        app:layout_constraintTop_toBottomOf="@+id/label"

        app:setEditable="@{viewModel.editable}"
        android:layout_marginEnd="8dp" />

</android.support.constraint.ConstraintLayout>

When a custom type field like the one above is preceded by a string type, the application crash when we tap on the next button of the keyboard while focusing the string type field.

The crash stacktrace:

java.lang.IllegalStateException: focus search returned a view that wasn't able to take focus!
        at android.widget.TextView.onKeyUp(TextView.java:7520)
        at android.view.KeyEvent.dispatch(KeyEvent.java:3361)
        at android.view.View.dispatchKeyEvent(View.java:10615)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1697)
        at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:590)
        at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1885)
        at android.support.v4.view.KeyEventDispatcher.activitySuperDispatchKeyEventPre28(KeyEventDispatcher.java:130)
        at android.support.v4.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:87)
        at android.support.v4.app.SupportActivity.dispatchKeyEvent(ComponentActivity.java:126)
        at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:535)
        at com.pascalwelsch.compositeandroid.activity.CompositeActivity.super_dispatchKeyEvent(CompositeActivity.java:2264)
        at com.pascalwelsch.compositeandroid.activity.ActivityDelegate.dispatchKeyEvent(ActivityDelegate.java:756)
        at com.pascalwelsch.compositeandroid.activity.CompositeActivity.dispatchKeyEvent(CompositeActivity.java:278)
        at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
        at android.support.v7.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2533)
        at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
        at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:467)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5041)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5003)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4532)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4585)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4551)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4684)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4559)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4741)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4532)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4585)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4551)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4559)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4532)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7092)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7024)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6985)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4181)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)

Finally here is the java class of DynamicRadioGroup:

public class DynamicRadioGroup extends ViewSwitcher {
    private CharSequence[] values;
    private int defaultValuePosition;
    private int orientation;
    private String selectedValue;
    private RadioGroup radioGroup;
    private TextView checkedOption;
    private OnValueChangeListener listener;

    public DynamicRadioGroup(Context context) {
        super(context);
        init();
    }

    /**
     * Initialize the view.
     */
    private void init() {
        //The ViewSwitcher will wrap its currently displayed layout.
        this.setMeasureAllChildren(false);

        //Tried this to fix the issue.
        this.setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);

        radioGroup = new RadioGroup(getContext());
        checkedOption = new TextView(getContext(), null, dynamicRadioGroupStyle);
        checkedOption.setGravity(Gravity.CENTER_VERTICAL);
        radioGroup.setOrientation(orientation);

        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            RadioButton radioButton = group.findViewById(checkedId);
            if (radioButton != null) {
                selectedValue = radioButton.getText().toString();
                checkedOption.setText(selectedValue);
                if (listener != null)
                    listener.onValuesChanged(this, selectedValue, checkedId);
            }
        });

        addView(radioGroup, 0);
        addView(checkedOption, 1);
    }

    //region GETTER & SETTER
    public void setValues(CharSequence[] values) {
        this.values = values;
        if(CollectionUtils.isNullOrEmpty(values)
            return;
        radioGroup.removeAllViews();
        for (int i = 0; i < this.values.length; i++) {
            RadioButton radioButton = new RadioButton(getContext());
            radioButton.setText(this.values[i]);
            radioButton.setId(i);
            if (radioGroup.getOrientation() == LinearLayout.HORIZONTAL)
                radioButton.setLayoutParams(new RadioGroup.LayoutParams(0, RadioGroup.LayoutParams.WRAP_CONTENT, 1.0f));
            radioGroup.addView(radioButton);
        }

    }

    public void setDefaultValuePosition(int defaultValuePosition) {
        this.defaultValuePosition = defaultValuePosition;
        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(this.defaultValuePosition);
        if (radioButton != null) {
            radioGroup.clearCheck();
            radioButton.setChecked(true);
        }
    }

    public void setEditable(boolean editable) {
        setDisplayedChild(editable ? 0 : 1);
        setFocusable(editable);
    }

    public String getSelectedValue() {
        return selectedValue;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

    public int getOrientation() {
        return orientation;
    }

    public void setOrientation(int orientation) {
        this.orientation = orientation;
        if (radioGroup != null)
            radioGroup.setOrientation(orientation);
    }

    public void setOnValueChangeListener(OnValueChangeListener listener) {
        this.listener = listener;
    }

    @Override
    protected boolean onRequestFocusInDescendants(final int dir, final Rect rect) {
        final View view = radioGroup.findViewById(radioGroup.getCheckedRadioButtonId());
        return view.requestFocus();
    }
    //endregion

}

As we can see I tried to play with the setDescendantFocusability method but without any success (maybe I'm doing wrong)?

I also tried to force setting next focus on the next RecyclerView item by using this code snippet:

@Override
public void onBindViewHolder(BaseFormViewHolder viewHolder, int index) {
    // (omitted stuffs)
    RecyclerView.ViewHolder previousViewHolder = getRecyclerView().findViewHolderForAdapterPosition(index -1);
    if(previousViewHolder != null)
        previousViewHolder.itemView.setNextFocusDownId(viewHolder.itemView.getId());

}

Another piece of information is that I used Stetho to analyse my layouts and I found under Accessibility Properties of the DynamicRadioGroup this message:

android focus search returned a view that wasn't able to take focus!

Not sure what to do about it but it may help.


TL;DR

Even if I don't get a full answer here I would like to better understand how this is working by getting answers to different questions:

  • How to make a custom View/custom ViewGroup focusable and react on the gain/lose of the focus.
  • How to force a View inside a a RecyclerView to point to the next item when talking about nextFocus.
  • What does the stacktrace above really mean?
  • What does the message inside the Stetho inspector really mean?

Thank you for reading!



Solution 1:[1]

This error occurs when the value of ime option EditorInfo.IME_ACTION_NEXT and the next object found is not focusable or its parent is not focusable.

As mentioned in the documentation of IME_ACTION_NEXT here

Bits of IME_MASK_ACTION: the action key performs a "next" operation, taking the user to the next field that will accept text.

Thus it calls to find the next item to which focus should be transferred, but this next view does not exist or isn't focusable or the parent cannot actually deal with the findFocus() call and returns null.

So, In your case if your custom class is extending from RadioGroup which extends LinearLayout, so you would need to make your custom view as clickable and focusable to true so it can handle the focus request.

Solution 2:[2]

I don't see following two functions in a constructor for your custom view or in xml and to make a view focusable you need these:

setFocusable(true);
setClickable(true);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Vasily Kabunov