1

I have a view where a user enters a goal, a date to complete it by, and a confirmation checkbox. A user must enter all of this to move forwards. Thus, I have checks to see if any of the fields are empty. Here is the code for that. This code works.

          private fun toggleInputRequiredError(
             isErrorVisible: Boolean,
             view: TextInputLayout,
             errorText: String
            ) {
              when (isErrorVisible) {
                  true -> {
                     view.error = errorText
                    }
                 else -> view.error = null
              }
           }

           toggleInputRequiredError(
                !state.isGoalChecked,
                goalAffirmationCheckBoxErrorContainer,
                getString(R.string.required_field)
            ).run {
                if (!state.isGoalChecked) {
                    goalAffirmationCheckBox.isFocusable = true
                    goalAffirmationCheckBox.requestFocus()
                    goalAffirmationCheckBox.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
                }
            }

            toggleInputRequiredError(
                !state.isValidDateInput,
                goalDateInputLayout,
                getString(R.string.required_field)
            ).run {
                if (!state.isValidDateInput) {
                    goalCompletionDateInput.isFocusable = true
                    goalCompletionDateInput.requestFocus()
                    goalCompletionDateInput.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
                }
            }

            toggleInputRequiredError(
                !state.isValidDescriptionInput,
                goalDescriptionInputLayout,
                getString(R.string.required_field)
            ).run {
                if( !state.isValidDescriptionInput) {
                    goalDescriptionInput.isFocusable = true
                    goalDescriptionInput.requestFocus()
                    goalDescriptionInput.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED)
                }
            }

Now I have to enable accessibility for this view with also works, on most phones except for the latest Samsung phones. The desired behaviour are exemplified by the Pixel XL but also the Samsung S8. Here is an image to show this

Pixel XL

On the newer Samsungs the sendAccessibilityEvent doesn't seem to actually focus on the view that needs to be addressed. Here is an image to show this behavior on the Samsung S10+ and Samsung Note 9.

Samsung S10+

I set the content description of these views in the XML. I noticed that the newer Samsung phones will read the "Required" text on the screen but not focus on it. This means that it ignores the content description in the XML. The last thing is that the view that needs to focusing seems not to experience the sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED) event

Let me know if you have any thoughts on how to address this or if you have any suggestions I can try

Princeps Polycap
  • 220
  • 2
  • 16

2 Answers2

0

In the Samsung S10 device, I can't able set accessibility focus to the custom view. Then I have achieved based on the following code so can try this with your follow maybe it would be helpful.

val task = Runnable {
        val mA11yManager = context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
        if (mA11yManager.isEnabled) {
            // Equivalent to ACTION_ACCESSIBILITY_FOCUS
            plus_chip_text?.performAccessibilityAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
            // we fire selection events here not in View
            plus_chip_text?.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED)
        }
    }
    val worker: ScheduledExecutorService = newSingleThreadScheduledExecutor()
    worker.schedule(task, 1, TimeUnit.SECONDS)
0

Sorry I know this is almost a year later, but we found a very similar issue on Samsung Talkback running Android 10. It would work fine on earlier versions of Android.

We found creating the following kotlin extension function seemed to work for all versions of Android, as well as working well for both Samsung Voice Assistant and Talkback.

Hope this is able to help anyone else facing a similar issue.

fun View?.requestAccessibilityFocus(): View? {
   this?.performAccessibilityAction(ACTION_ACCESSIBILITY_FOCUS, null)
   this?.sendAccessibilityEvent(TYPE_VIEW_SELECTED)
   return this
}
dev2505
  • 372
  • 4
  • 10