Shyam Sunder
Shyam Sunder

Reputation: 164

Override default talkback announcement of SeekBar

Applying accessibility content description for Seekbar in setOnSeekBarChangedListener and talkback is reading the seek bar rating status in percentage then the content description eg "100% 10 rating very satisfied"

What I did Calling this function from onProgressChanged and onStopTrackingTouch

Here is code snapshot of function

void setAccessibilityTextForView(View mSeekBar, String msg) {
    if (AccessibilityHandler.isAccessibilityEnabled(this)) {
        if (mSeekBar != null) {
            mSeekBar.setContentDescription(msg)

        }
    }
}

Actual Result : "100% 10 Rating very satisfied" Expectation: Talkback should ready only the content description I've applied eg "10 rating very satisfied"

Upvotes: 2

Views: 139

Answers (1)

Quintin B
Quintin B

Reputation: 5881

I would recommend you rather use the documentation for stateDescription API:

A state description briefly describes the states of the view and is primarily used for accessibility support to determine how the states of a view should be presented to the user. It is a supplement to the boolean states (for example, checked/unchecked) and it is used for customized state description (for example, "wifi, connected, three bars"). State description changes frequently while content description should change less often.

It seems to indicate that stateDescription should be the one that gets updated more regularly.

I don't know what version of slider you are using or why it's not a RatingBar (which I know in XML Views inherits from SeekBar) - but I think I can help you out anyway.

I think the problem is that you aren't setting the stateDescription:

  1. on init
  2. on change
    private fun setSeekBarStateDescription() {
        ViewCompat.setStateDescription(
            seekbar,
            when {
                seekbar.progress <= 20 -> "1 star"
                seekbar.progress in 21..40 -> "2 star"
                seekbar.progress in 41..60 -> "3 star"
                seekbar.progress in 61..80 -> "4 star"
                else -> "5 stars"

            }
        )
    }
    

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rating_bar)

        ...

        seekbar.apply {
            setSeekBarStateDescription()
            setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    setSeekBarStateDescription()
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit

                override fun onStopTrackingTouch(seekBar: SeekBar?) = Unit
            })
        }
    }

My XML looks like this:

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            />

And the output I get seems to be what you are expecting:

A simple Android app with a rating bar - up and down gestures are used to demonstrate the rating bar being changed, and TalkBack's announcement is '1 star', '2 star' in relation to the position of the slider

Upvotes: 2

Related Questions