Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between android:contentDescription="@null" and tools:ignore="ContentDescription"? Use android:importantForAccessibility="no"?

Take this android layout XML snippet for example:

<ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:padding="10dp"
        app:srcCompat="@drawable/bitcoin"
        android:contentDescription="@null"
        android:importantForAccessibility="no"
        tools:ignore="ContentDescription" />

What is the difference between android:contentDescription="@null" and tools:ignore="ContentDescription"?

I know both of them are used to indicate that a certain non-textual element carries no meaning and is only meant for decoration. Is there an advantage of using one over the other, should I use both, is it preference, or is one considered better and newer than the other?

Also, should I use android:importantForAccessibility="no" or is using all three attributes/properties simply overkill?

like image 258
Shikhar Mainalee Avatar asked Jan 01 '23 05:01

Shikhar Mainalee


1 Answers

What is the difference between android:contentDescription="@null" and tools:ignore="ContentDescription"?

android:contentDescription="@null"

  • Used to indicate that a certain non-textual element carries no meaning and is only meant for decoration.

tools:ignore="ContentDescription"

  • For graphical elements, such as ImageView and ImageButton. If you do not set their respective android:contentDescription XML attributes, a lint warning message will be displayed.

    "Missing contentDescription attribute on image"

  • To suppress this lint warning message then you must use tools:ignore="ContentDescription" in XML.

I know both of them are used to indicate that a certain non-textual element carries no meaning and is only meant for decoration. Is there an advantage of using one over the other, should I use both, is it preference, or is one considered better and newer than the other?

No, they are different from each other in term of usage, for example

<ImageView 
    android:layout_width="200dp"
    android:layout_height="300dp"
    android:id="@+id/image_user_avatar"
    android:contentDescription="User avatar"
    tools:ignore="ContentDescription" />

When running the app with TalkBack, it will speak "User avatar".

Should I use android:importantForAccessibility="no"?

If your app only supports devices running Android 4.1 (API level 16) or higher, you can set these elements' android:importantForAccessibility XML attributes to "no" instead of android:contentDescription="@null.

Update

So basically tools:ignore="ContentDescription" is only for the compiler and android:contentDescription="@null" is for user user?

Yes, it is.

Also, my 'minSdk' is 14 and my 'targetSdk' is 28. Can I still set both android:importantForAccessibility="no" and android:contentDescription="@null"?

Yes, you can set both of them but if you run the app on device whose SDK below 16, android:importantForAccessibility="no" will be ignored.

Will android:contentDescription="@null" have the same effect as android:importantForAccessibility="no" for devices running Android 4.1 or higher?

They have slightly difference.

android:contentDescription="@null": The view with this attribute still highlighted when users move finger on it and Accessibility Services will speak out loud dummy text such as "Button", etc.

android:importantForAccessibility="no": The view with this attribute is disabled by the app so it will not highlighted when users move finger on and ignored by Accessibility Services as well.

like image 82
Son Truong Avatar answered Jan 03 '23 17:01

Son Truong