Adding content descriptions
You can set a widget’s content description in the XML layout file by setting a value for the attribute
android:contentDescription. That is what you are going to do next. You can also set it in your UI
setup code, using someView.setContentDescription(someString), which you will do later in this
chapter.
The text you set should be meaningful without being overly wordy. Remember, TalkBack users will
be listening to the audio, which is linear. They can speed up the pace of TalkBack’s speech output, but
even so you want to avoid adding extraneous information and wasting users’ time. For example, if you
are setting the description for a framework widget, avoid including information about what kind of
widget it is (e.g., a button), because TalkBack already knows and includes that information.
First, some housekeeping. Add the following strings to the unqualified strings.xml.
Listing 19.1 Adding content description strings (res/values/strings.xml)
...
Crime scene photo (not set)
...
Next, open res/layout/fragment_crime.xml and set the content description for both the
ImageButton and ImageView.
Listing 19.2 Setting content descriptions for ImageView and ImageButton
(res/layout/fragment_crime.xml)
<ImageView
android:id="@+id/crime_photo"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@android:color/darker_gray"
android:cropToPadding="true"
android:scaleType="centerInside"
android:contentDescription="@string/crime_photo_no_image_description" />
<ImageButton
android:id="@+id/crime_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_camera"
android:contentDescription="@string/crime_photo_button_description" />
Run CriminalIntent and press the camera button. TalkBack helpfully announces, “Take photo of crime
scene button. Double-tap to activate.” This spoken information is much more helpful than “button
unlabeled.”
Next, press the crime scene image (which at the moment is just the gray placeholder). You might
expect the accessibility focus to move to the ImageView, but the green border appears around the entire
fragment and TalkBack announces overview information about the fragment instead of about the
ImageView. What gives?