I'm trying to ensure that keyboard navigation works properly on my app and all view elements are focusable when users hit the 'Tab' key or 'Up/Down' arrow keys. However, the AppBarLayout seems to trap keyboard focus. I'm running my app on an emulator with a keyboard configured. A sample layout is as below:
<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"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/search_fragment_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical"
android:scrollbars="horizontal"
android:focusable="true"
android:nextFocusDown="@id/recycler_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Sample text 1"
android:focusable="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Sample text 2"
android:focusable="true"/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:focusable="true"
app:layout_constraintTop_toBottomOf="@layout/content_main"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
Hitting the Tab key or the Up/Down arrows does not switch the focus over to the recycler view and the focus is stuck within the AppBarLayout. Setting the nextFocusDown attribute on the textviews and setting the view elements to remain focusable does not work either. Here is the image. What do I need to do to make this layout accessible?
I think you need to call
findViewById(R.id.search_fragment_app_bar).setTouchscreenBlocksFocus(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
findViewById(R.id.search_fragment_app_bar).setKeyboardNavigationCluster(false);
}
setTouchscreenBlocksFocus
setKeyboardNavigationCluster
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With