Imagine you have a LinearLayout
inside a RelativeLayout
that contains 3 TextViews
with artist, song and album
:
<RelativeLayout
...
<LinearLayout
android:id="@id/text_view_container"
android:layout_width="warp_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@id/artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artist"/>
<TextView
android:id="@id/song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Song"/>
<TextView
android:id="@id/album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="album"/>
</LinearLayout>
<TextView
android:id="@id/unrelated_textview1/>
<TextView
android:id="@id/unrelated_textview2/>
...
</RelativeLayout>
When you activate the TalkbackReader and click on a TextView
in the LinearLayout
, the TalkbackReader will read "Artist", "Song" OR "Album" for example.
But you could put those first 3 TextViews
into a focus group, by using:
<LinearLayout
android:focusable="true
...
Now the TalkbackReader would read "Artist Song Album".
The 2 unrelated TextViews
still would be on their own and not read, which is the behaviour I want to achieve.
(See Google codelabs example for reference)
I am now trying to re-create this behaviour with the ConstrainLayout
but dont see how.
<ConstraintLayout>
<TextView artist/>
<TextView song/>
<TextView album/>
<TextView unrelated_textview1/>
<TextView unrelated_textview2/>
</ConstraintLayout>
Putting widgets into a "group" does not seem to work:
<android.support.constraint.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:importantForAccessibility="yes"
app:constraint_referenced_ids="artist,song,album"
/>
So how can I re-create focus-groups for accessibility in the ConstrainLayout
?
[EDIT]: It seems to be the case, that the only way to create a solution is to use "focusable=true" on the outer ConstraintLayout and / or "focusable=false" on the views themselves. This has some drawbacks that one should consider when dealing with keyboard navigation / switch-boxes:
https://github.com/googlecodelabs/android-accessibility/issues/4
Android introduced
android:screenReaderFocusable
to group contents in constraint layout. This will work for the above mentioned case. But requires API level 27.https://developer.android.com/guide/topics/ui/accessibility/principles#content-groups
The focus groups based upon
ViewGroups
still work withinConstraintLayout
, so you could replaceLinearLayouts
andRelativeLayouts
withConstraintLayouts
and TalkBack will still work as expected. But, if you are trying to avoid nestingViewGroups
withinConstraintLayout
, keeping with the design goal of a flat view hierarchy, here is a way to do it.Move the
TextViews
from the focusViewGroup
that you mention directly into the top-levelConstraintLayout
. Now we will place a simple transparentView
on top of theseTextViews
usingConstraintLayout
constraints. EachTextView
will be a member of the top-levelConstraintLayout
, so the layout will be flat. Since the overlay is on top of theTextViews
, it will receive all touch events before the underlyingTextViews
. Here is the layout structure:We can now manually specify a content description for the overlay that is a combination of the text of each of the underlying
TextViews
. To prevent eachTextView
from accepting focus and speaking its own text, we will setandroid:importantForAccessibility="no"
. When we touch the overlay view, we hear the combined text of theTextViews
spoken.The preceding is the general solution but, better yet, would be an implementation of a custom overlay view that will manage things automatically. The custom overlay shown below follows the general syntax of the
Group
helper inConstraintLayout
and automates much of the processing outlined above.The custom overlay does the following:
Group
helper ofConstraintLayout
.View.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO)
on each view. (This avoids having to do this manually.)contentDescription
,getText()
or thehint
. (This avoids having to do this manually. Another advantage is that it will also pick up any changes made to the text while the app is running.)The overlay view still needs to be positioned manually within the layout XML to overlay the
TextViews
.Here is a sample layout showing the
ViewGroup
approach mentioned in the question and the custom overlay. The left group is the traditionalViewGroup
approach demonstrating the use of an embeddedConstraintLayout
; The right is the overlay method using the custom control. TheTextView
on top labeled "initial focus" is just there to capture the initial focus for ease of comparing the two methods.With the
ConstraintLayout
selected, TalkBack speaks "Artist, Song, Album".With the custom view overlay selected, TalkBack also speaks "Artist, Song, Album".
Below is the sample layout and the code for the custom view. Caveat: Although this custom view works for the stated purpose using
TextViews
, it is not a robust replacement for the traditional method. For example: The custom overlay will speak the text of view types extendingTextView
such asEditText
while the traditional method does not.See the sample project on GitHub.
activity_main.xml
AccessibilityOverlay.java
attrs.xml
Define the custom attributes for the custom overlay view.
Set Content Description
Make sure the
ConstraintLayout
is set to focusable with an explicit content description. Also, make sure the childTextViews
are not set to focusable, unless you want them to be read out independently.XML
Java
If you'd rather set the ConstraintLayout's content description dynamically in code, you can concatenate the text values from each relevant
TextView
:Accessibility Results
When you turn Talkback on, the ConstraintLayout will now take focus and read out its content description.
Screenshot with Talkback displayed as caption:
Detailed Explanation
Here is the full XML for the above example screenshot. Notice that focusable and content description attributes are set only in the parent ConstraintLayout, not in the child TextViews. This causes TalkBack to never focus on the individual child views, but only the parent container (thus, reading out only the content description of that parent).
Nested Focus Items
If you want your unrelated TextViews to be focusable independent of the parent ConstraintLayout, you can set those TextViews to
focusable=true
as well. This will cause those TextViews to become focusable and read out individually, after the ConstraintLayout.If you want to group the unrelated TextViews into a singular TalkBack announcement (separate from the ConstraintLayout), your options are limited:
ViewGroup
, with its own content description, orfocusable=true
only on the first unrelated item and set its content description as a single announcement for that sub-group (e.g. "unrelated items").Option #2 would be considered a bit of a hack, but would allow you to maintain a flat view hierarchy (if you really want to avoid nesting).
But if you are implementing multiple sub-groupings of focus items, the more appropriate way would be to organize the groupings as nested ViewGroups. Per the Android accessibility documentation on natural groupings:
I ran into the same issue recently and I decided to implement a new Class using the new ConstraintLayout helpers (available since constraintlayout 1.1) so that we can use it in the same way that we use the Group view.
The implementation is a simplified version of Cheticamp's answer and his idea of creating a new View that would handle the accessibility.
Here is my implementation:
Also available as a gist: https://gist.github.com/JulienArzul/8068d43af3523d75b72e9d1edbfb4298
You would use it the same way that you use a Group:
This sample organises the TextView and ImageView in a single group for accessibility purposes. You can still add other Views that take the focus and are read by the Accessibility reader inside the ConstraintLayout.
The View is transparent but you can choose the area it is displayed on when focused by using the regular constraint layout attributes.
In my example, the accessibility group is displayed over the full ConstraintLayout but you could choose to align it with some or all of your referenced views by modifying the
app:"layout_constraint..."
attributes.EDIT: As suggested by @Mel' in the comments, I updated the
ConstraintLayoutAccessibilityHelper
class to make sure only visible Views are added in the Accessibility event.Set the constraint layout as focusable (by setting android:focusable="true" in constraint layout)
Set content description to Constraint Layout
set focusable="false" for views that are not to be included.
Edit Based on Comments Only applicable if there is single focus group in constraint layout.