Setting contentDescription from label for android accessibility

accessibility, android

Solution

If you already know the value of the `TextView`, you can use that:

<LinearLayout>
    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@string/some_str" />
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/some_str" />
</LinearLayout>

However, if you are setting the `TextView`'s contents programmatically, you will need to do the same for the `ImageView`'s content description. (See this documentation.)

String desc = "Foo Bar";
yourTextView.setText(desc);
yourImageView.setContentDescription(desc);

Problem

While a constant `contentDescription` is appropriate for some widgets, other cases such as an image gallery with captions already have appropriate accessibility labels in the UI. Is there any way to link an `ImageView`'s `contentDescription` with its labelling `Textview` in XML? For example, is there a real equivalent for the last (fictional) line of this `ImageView` definition: ``` <LinearLayout> <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:contentDescription="@+id/label" /> <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> ```

Original source