Search code examples
androidimageviewaccessibilityandroid-imageviewtalkback

How to disable/Manage Accessibility announcement on ImageView? Android


My Screen has Image and Titleview. I need to announce talkback as the Title text when the page loads. But it always announces the contentDescription of an ImageView.

View Hierarichy

ImageView and then title.

What I tried ?

Tried to set focus on title, For that I set these attributes to ImageView

android:focusable="false"
android:accessibilityTraversalAfter="@id/title"
android:importantForAccessibility="no"

Bad luck, none of these worked in my case. As a second try, I added these in my fragment

onResume {

    if (accessibilityManager?.isEnabled == true) {
        activity.title = title
        activity.window.decorView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED)
    }
}

Still not working !! Does contentDescription of an ImageView has this much power?

I need to announce the title text when the page loads and if the user taps (or swipe left/right)then Imageview description needs to announce

XML ....

<data>

.....

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/vertical_padding"
    android:theme="@style/CL.Theme.Light">

    <ImageView
        android:id="@+id/close_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_close"
        android:importantForAccessibility="no"
        android:contentDescription="CLOSE"
        android:accessibilityTraversalAfter="@id/title"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/back_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="BACK"
        android:src="@drawable/icon_back"
        android:importantForAccessibility="no"
        android:accessibilityTraversalAfter="@id/title
        android:layout_margin="@dimen/icon_margin"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.core.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/back_button"
        app:layout_constraintBottom_toTopOf="@id/primary_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top|clip_horizontal"
            android:paddingStart="@dimen/horizontal_padding"
            android:paddingTop="@dimen/error_screen_top_offset"
            android:paddingEnd="@dimen/horizontal_padding"
            android:paddingBottom="@dimen/vertical_padding">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/margin_top"
                android:gravity="center"
                android:text="HEADER TITLE"
                android:focusable="true" 
                android:focusableInTouchMode="true"
                android:importantForAccessibility="yes"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_goneMarginTop="62dp"
                tools:text="Title" />

            <TextView
                android:id="@+id/subtitle
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/subtitle_margin_top"
                android:gravity="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/title"
                tools:text="Subtitle" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/cta_margin_top"
        android:layout_marginStart="@dimen/horizontal_padding"
        android:layout_marginEnd="@dimen/horizontal_padding"
        android:layout_marginBottom="@dimen/secondary_cta_margin"
    />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="@dimen/horizontal_padding"
        android:layout_marginEnd="@dimen/horizontal_padding"
        android:gravity="center"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Found better solutions from a different StackOverflow question

    Hope this is able to help anyone else facing a similar issue.

    fun View?.requestAccessibilityFocus(): View? {
       this?.performAccessibilityAction(ACTION_ACCESSIBILITY_FOCUS, null)
       this?.sendAccessibilityEvent(TYPE_VIEW_SELECTED)
       return this
    }