ChatGPT解决这个技术问题 Extra ChatGPT

'Missing contentDescription attribute on image' in XML

I get an warning about [Accessibility]Missing contentDescription attribute on image in eclipse. This warning show at line 5 (declare ImageView) in XML code below.

This not make any error when build and run my application. But I really want to know why i get this warning.

This is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/contact_entry_image"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/contact_entry_text"
        android:text=""
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        />

</LinearLayout>

Please help me regarding this and thanks for your reading.

the new ADT version suggestion add android:contentDescription this attribute in imageview, just a suggestion, i think you can ignore this, it dont make the compile error, also you can check http://developer.android.com/reference/android/view/View.html#attr_android:contentDescription
yes that is right

C
Community

Follow this link for solution: Android Lint contentDescription warning

Resolved this warning by setting attribute android:contentDescription for my ImageView android:contentDescription="@string/desc" Android Lint support in ADT 16 throws this warning to ensure that image widgets provide a contentDescription This defines text that briefly describes the content of the view. This property is used primarily for accessibility. Since some views do not have textual representation this attribute can be used for providing such. Non-textual widgets like ImageViews and ImageButtons should use the contentDescription attribute to specify a textual description of the widget such that screen readers and other accessibility tools can adequately describe the user interface.

This link for explanation: Accessibility, It's Impact and Development Resources

Many Android users have disabilities that require them to interact with their Android devices in different ways. These include users who have visual, physical or age-related disabilities that prevent them from fully seeing or using a touchscreen. Android provides accessibility features and services for helping these users navigate their devices more easily, including text-to-speech, haptic feedback, trackball and D-pad navigation that augments their experience. Android application developers can take advantage of these services to make their applications more accessible and also build their own accessibility services.

This guide is for making your app accessible: Making Apps More Accessible

Making sure your application is accessible to all users is relatively easy, particularly when you use framework-provided user interface components. If you only use these standard components for your application, there are just a few steps required to ensure your application is accessible: Label your ImageButton, ImageView, EditText, CheckBox and other user interface controls using the android:contentDescription attribute. Make all of your user interface elements accessible with a directional controller, such as a trackball or D-pad. Test your application by turning on accessibility services like TalkBack and Explore by Touch, and try using your application using only directional controls.


One more important note! In this case, it looks like the image is purely decorative and does not provide additional information to the user. So, for example, you wouldn't want to speak "Contact image" to the user but you may want to speak "Bob's profile photo" if you don't show that text anywhere else. If an image shouldn't be spoken to the user, you can set android:contentDescription="@null" and the warning will go away.
@alanv: great comment. Much better than disabling the warning completely as is commonly advised.
Actually I don't think android:contentDescription="@null" is the best solution. I'm using tools:ignore="ContentDescription" that is what is meant to be. Make sure you include xmlns:tools="schemas.android.com/tools" in your root layout.
thank you for providing such nice document. when you have may images like wallpaper gallery app, is it suitable to add content description for all images ? and also can it impact on app performance ?
That´s right when using certain types of Views in an interface, they must include content tags that describe the purpose or action associated with that View.
J
Jeroen

Add android:contentDescription="@string/description" (static or dynamic) to your ImageView. Please do not ignore nor filter the message, because it is helpfull for people using alternative input methods because of their disability (Like TalkBack, Tecla Access Shield etc etc).


@Jeroen- When i added description, it is saying: "Couldn't resolve resource @string/loginText"- Why is that?
Do you have a loginText string in the string XML?
@JamalZafar create a string in string.xml with the name loginText
generally the pattern should be @string/accessibility_your_description
a
arekolek

Updated:

As pointed out in the comments, setting the description to null indicates that the image is purely decorative and is understood as that by screen readers like TalkBack.

Old answer, I no longer support this answer:

For all the people looking how to avoid the warning:

I don't think android:contentDescription="@null" is the best solution.

I'm using tools:ignore="ContentDescription" that is what is meant to be.

Make sure you include xmlns:tools="http://schemas.android.com/tools" in your root layout.


This one should be upvoted because it's also what Google recommended
Using @null as a contentDescription is a way of explicitly saying that this image is purely decorative. Simply ignoring the warning is not the correct solution.
Using @null tells TalkBack that the image is decorative. You shouldn't ignore the warning as putting @null informs TalkBack rather than making it think it's missing and should be there.
a
arekolek

Going forward, for graphical elements that are purely decorative, the best solution is to use:

android:importantForAccessibility="no"

This makes sense if your min SDK version is at least 16, since devices running lower versions will ignore this attribute.

If you're stuck supporting older versions, you should use (like others pointed out already):

android:contentDescription="@null"

Source: https://developer.android.com/guide/topics/ui/accessibility/apps#label-elements


M
Mwongera808

Add

tools:ignore="ContentDescription"

to your image. Make sure you have xmlns:tools="http://schemas.android.com/tools" . in your root layout.


+1. This is Android Studio's fix for this problem (when you perform code analyze, the "suppress Lint" button does just that)
DO NOT ignore content description. You need to determine what the imageview represents: decorative, or something important. Take a look at the other options here.
A
AlexR

The warning is indeed annoying and in many (most!) cases no contentDescription is necessary for various decorative ImageViews. The most radical way to solve the problem is just to tell the Lint to ignore this check. In Eclipse, go to "Android/Lint Error Checking" in Preferences, find "contentDescription" (it is in the "Accessibility" group) and change "Severity:" to Ignore.


Do not ignore this warning. As others have pointed out accessibility is important even if it's just a decorative image. It's no big deal to add the desired contentDescription
You may as well ignore the warning if your app is one that will be essentially inaccessible even with content descriptions. For example, why bother with contentDescription in a game that requires sight to be able to play?
Yes, that is the radical choice; so what do folks do who are aware of it's purpose and would like to comply? I would venture to say that in this particular case, forget "most" cases; that seems to be implying that because "most" people aren't, for example, blind it's a needless warning? If "most" of your images need no such content - fine, disable the warning, but add it where it would make sense. Being a little helpful can go a long way.
It's also worth bearing in mind that many people who use screen readers or assisstive technology do have at least some sight and so may be able to play a game, but not be able to read or see certain text, icons, images or colours. So having relevant content descriptions may help people even if you can't forsee it.
a
alap

If you don't care at all do this:

    android:contentDescription="@null"

Although I would advise the accepted solutions, this is a hack :D


Actually I don't think android:contentDescription="@null" is the best solution. I'm using tools:ignore="ContentDescription" that is what is meant to be. Make sure you include xmlns:tools="schemas.android.com/tools" in your root layout.
No one said that. Read the comment again. You just ignore the warning so the solution that you provide is definitly worse. Read the most accepted answer again.
M
Maksim Turaev

This warning tries to improve accessibility of your application.

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

android {

    ...

    lintOptions {
        disable 'ContentDescription'
    }
}

C
Community

It is giving you the warning because the image description is not defined.

We can resolve this warning by adding this code below in Strings.xml and activity_main.xml

Add this line below in Strings.xml

<string name="imgDescription">Background Picture</string>
you image will be like that:
<ImageView
        android:id="@+id/imageView2"
        android:lay`enter code hereout_width="0dp"
        android:layout_height="wrap_content"
        android:contentDescription="@string/imgDescription"
        app:layout_editor_absoluteX="0dp"
        app:layout_editor_absoluteY="0dp"
        app:srcCompat="@drawable/background1"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />

Also add this line in activity_main.xml

android:contentDescription="@string/imgDescription"

Strings.xml

<resources>
    <string name="app_name">Saini_Browser</string>
    <string name="SainiBrowser">textView2</string>
    <string name="imgDescription">BackGround Picture</string>
</resources>

Thanks, rather then ignore the warning everyone should understand what's going on thanks Mr. Kuldeep Singh
A
Aniket Jain

Also add this line in XML

android:contentDescription="@null"

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/contact_entry_image"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@null"
        />
    <TextView
        android:id="@+id/contact_entry_text"
        android:text=""
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        />

</LinearLayout>


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now