I am setting up a function that will deliver sounds based on the item currently selected by the Talk Back accessibility service. My issue is that I can not find a way to actually ask Android what has been currently selected by TalkBack.
-
Why do you want to know what is currently selected? You can just add `android:contentDescription="@string/description_msg"` and TalkBack would do the rest for you. – ankuranurag2 Jan 10 '19 at 14:08
-
There are two parts of the screen: video and chat. When a user has a chat element selected, I want sounds to play, none when no chat element is played. This is so users are aware that new chat messages are appearing when engaged with chat. I am aware of setting content descriptions, but I won't want item descriptions read before reading every chat message. – kamehouseorbust Jan 10 '19 at 14:11
2 Answers
You can use the android Text-To-Speech Engine(TTS) provided by the system. Using this, you can play the audio of the text provided,on a new message event or whenever user clicks on the text.
TextToSpeech tts=new TextToSpeech(this,this);
String text = "message_text_string";
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

- 2,300
- 15
- 30
I don't know the specific answer for android, perhaps you can, but generally you can't know where a screen reader user is currently looking.
You must make the difference between system focus, and the current reading position. They aren't the same.
When the system focus is moved, for example programatically or when pressing tab on a keyboard, in principle the reading cursor follows. The converse isn't true when the user reads the page with arrow keys or by sweeping on mobile devices. IN that case, the reading position moves but the system focus stays in place.
You can know where the system focus is at any time (example in JavaScript: document.activeElement
), but there is generally no equivalent to know where the reading cursor is.
Philosophically it's very good as it is. If we take a paralel, you have no way to know where exactly the user is looking at on the screen (except with very specific hardware).
For your particular case, the best you can do is probably to base on the system focus to decide whether or not to play sound.

- 12,311
- 4
- 24
- 37