Search code examples
androidaccessibilityclickablespannablestringtalkback

How can I use Androids Talkback with clickableSpan?


So I have a text of variable length and until now I have filled it with a SpannableString to highlight specific "hard" words you can then click on to get an explanantion in a dialog. But because I have to design my application for accessibility I neeed androids Talkback feature to read out these words (plus the text surrounding it, but I've got that for now) as well as being able to click them. So far I haven't found a way even to click on the ClickableSpan without disabling Talkback.

I found something about ClickableSpan not being able to handle Acessibility but URLSpan is? If that is the case, can I open a dialog with custom text with a URLSpan? Or does it have something to do with me calling text.setMovementMethod(LinkMovementMethod.getInstance()); ?

Thanks in advance, it's been really hard to find anything on Accessibility, not many programmers seem to care much.


Solution

  • So I finally did find a way to use Talkback with SpannableStrings. Well, not really, but it's a workaround. I removed the ClickableSpan from the TextView but stored the start and end positions and put the textView into another Layout.

    I then iterated through the stored positions and added empty Views right on top of the text with a fitting ContentDescription and the onClick property I needed in it's onClickListener. To get the Views position and size I used this code:

    //clickableSpans is an int[] Array with the start and end character positions of the previous clickableSpans stored after each other
    
    for (int i = 0; i < clickableSpans.length; i +=2) {
        int width= (int) (layout.getPrimaryHorizontal(clickableSpans[i+1]) - (int) layout.getPrimaryHorizontal(clickableSpans[i]));
        int height= layout.getLineBottom(layout.getLineForOffset(clickableSpans[i])) - layout.getLineTop(layout.getLineForOffset(clickableSpans[i]));
    
        int xOffset = (int) layout.getPrimaryHorizontal(clickableSpans[i]);
        int yOffset = layout.getLineTop(layout.getLineForOffset(clickableSpans[i]));
    
        //Now position an empty View according to those values
    }