Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to eliminate warning in android layout.xml: "missing contentDescription"

<ImageView style="@style/LoaderPrevNext.Next" />

using the styles

<style name="LoaderPrevNext">
    <item name="android:layout_width">40dp</item>
    <item name="android:layout_height">80dp</item>
    <item name="android:layout_weight">0</item>
    <item name="android:scaleType">fitXY</item>
</style>

<style name="LoaderPrevNext.Next">
    <item name="android:src">@drawable/next_page</item>
    <item name="android:contentDescription">@string/img_desc_next_page</item>
</style>

annoys me with the [Accessibility] Missing contentDescription attribute on image warning.

It disappears if I move or copy the contentDescription from the style to the ImageView tag, but as the src is defined in the style, I'd love to keep them together.

Guess it's simply an SDK bug, but some might got a workaround...

Clarification: The ImageView in question do have a content-description, defined in the LoaderPrevNext.Next style. The warning is false. I'm not interested in ideas on how to set the content description, or how to hack them empty.

like image 388
vmatyi Avatar asked Feb 20 '12 14:02

vmatyi


5 Answers

In Eclipse Window->Preferences->Android->Lint Erroe Checking->Right Side Scroll Down Until Accessibility->Content Description->Severity->Select Ignore->Apply->yes

like image 61
Samir Mangroliya Avatar answered Oct 24 '22 17:10

Samir Mangroliya


How To Suppress Android Accessibility Warning Missing contentDescription

Both of the existing answers to this question are technically correct, but I believe there is a better way. The first answer suggests turning off all contentDescription warnings. While this works, contentDescription is there for a reason, and perhaps should not be globally turned off for all content.

The second answer shows how to specify the contentDescription. While this does make the warning go away, it is not appropriate for all content and technically does not answer the question, although it is a valid work-around.

The Android documentation for lint provides the best solution, IMO, which is a mixture of providing contentDescription for some content, and suppressing the lint warning for other content:

Note that elements in application screens that are purely decorative and do not provide any content or enable a user action should not have accessibility content descriptions. In this case, just suppress the lint warning with a tools:ignore="ContentDescription" attribute.

Therefore, Implement the following solution to remove all of the lint warnings:

  • Define contentDescription in your XML layout file with android:contentDescription="Your description here." for resources that provide interesting or useful information to the user. And,
  • Add tools:ignore="ContentDescription" to purely decorative content or images.
like image 24
David Manpearl Avatar answered Oct 24 '22 17:10

David Manpearl


To disable missing content description warning in the whole project, add this to your build.gradle

android {

    ...

    lintOptions {
        disable 'ContentDescription'
    }
}
like image 42
Maksim Turaev Avatar answered Oct 24 '22 18:10

Maksim Turaev


If you do not wish to turn off / ignore the lint warnings you could define an empty string in strings.xml

string.xml:

<string name="empty"></string>

and then in your xml just set the value

<ImageView
    android:id="..."
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:contentDescription="@string/empty"/>
like image 40
Johie Avatar answered Oct 24 '22 19:10

Johie


contentDescription tag is used for screen reading feature of android. you can add a string about what is your image is or you can just suppress/ignore this warning by adding the following tag.

tools:ignore="ContentDescription"
like image 38
Rajneesh Shukla Avatar answered Oct 24 '22 19:10

Rajneesh Shukla