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

9

8
<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.

Printable answered 20/2, 2012 at 14:59 Comment(1)
#9731173Hinayana
G
6

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

Graziano answered 20/2, 2012 at 15:9 Comment(1)
Didn't meant to suppress all these warnings. Only the faulty ones.Printable
L
6

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.
Larainelarboard answered 24/3, 2014 at 19:11 Comment(3)
Hi! The ImageView in question do have a content-description, defined in the LoaderPrevNext.Next style.Printable
1. You must prece "tools:ignore", with the line: xmlns:tools="schemas.android.com/tools" 2. If you have LOTS of purely decorative images, you can put the tools:ignore="ContentDescription" at higher levels in the View hierachy.Rupiah
@Printable Maybe you can accept this answer. According to Technical docs‎ of Tools Attributes, "Android has a dedicated XML namespace intended for tools to be able to record information in XML files, and have that information stripped when the application is packaged such that there is no runtime or download size penalty. The namespace URI is http://schemas.android.com/tools and is usually bound to the tools: prefix".Intertexture
E
4

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

android {

    ...

    lintOptions {
        disable 'ContentDescription'
    }
}
Eyra answered 3/4, 2018 at 20:53 Comment(0)
M
1

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"/>
Mullion answered 11/6, 2014 at 9:20 Comment(1)
Hi! The ImageView in question do have a content-description, defined in the LoaderPrevNext.Next style. I do NOT want it to be empty. It is set (in the style), generating a false warning.Printable
A
1

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"
Aalii answered 25/5, 2020 at 5:50 Comment(0)
E
1

I you are using old versions, you should use

android:contentDescription="@null"

Or:

android:importantForAccessibility="no"
Electrophotography answered 5/6, 2024 at 15:20 Comment(0)
A
0

< ImageView

android:id= ...

android:layout_width= ... ...

android:contentDescription="your description comes here. get from strings xml if its a string"

/>

Absenteeism answered 4/11, 2012 at 6:56 Comment(0)
W
0

Why would you want to have image description in Style.

The purpose of styles is to have an ability to change something in one place which is reflected in several places.

Same goes for @string resource. Simply don't put image description in style. Leave @string. Once needed - change that string itself.

Woolsack answered 11/10, 2017 at 15:18 Comment(0)
B
0

I think that the correct solution is to report this as a bug in Android Studio, though I doubt that they will fix it anytime soon (from experience with my bug reports about other things). https://developer.android.com/studio/report-bugs

I'm sorry that so far you have had only off-topic answers that totally ignore the key elements of your question, and answers that claim that styles aren't meant for doing what you want to them (even though the docs never warn against what you are trying to do). I came here because I have the same problem as you have (in 2023 and which I encountered around 2020). I don't see any sign of Android Studio / Android Lint offering an option for taking styles into account. It doesn't even look like there is a point to not taking styles into account, therefore there shouldn't be an option for this, any rule that checks presence or content of specific attributes should also look up the style specified by style="".

Bowel answered 15/1, 2023 at 20:48 Comment(1)
"I thinik" should be commentnot an answerLecher

© 2022 - 2025 — McMap. All rights reserved.