4

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.

Aram Becker
  • 2,026
  • 1
  • 20
  • 32
  • 1
    TalkBack does not currently support `ClickableSpan`. It supports `URLSpan` by launching an intent for the URL -- it does not go through the normal code path for clicking spannables. You will need to find some alternate way to expose the clickable items. – alanv Aug 21 '15 at 16:28
  • @alanv Yeah, thought that, but nice to be sure :) – Aram Becker Aug 22 '15 at 01:28

3 Answers3

1

Talkback users will use local context menu to access links within text: https://support.google.com/accessibility/android/answer/6007066?hl=en

  • 6
    While the link you provided may answer the question, it is best to put the essential parts of your solution directly in your answer in case the link expires in the future. – Kmeixner Mar 08 '16 at 21:15
  • Samsung has no local context menu. – Simas Jul 02 '19 at 12:47
1

Maybe this can be the easiest solution..it worked for me.

textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ClassroomLog.log(TAG, "Textview Click listener ");
        if (textView.getSelectionStart() == -1 && textView.getSelectionEnd() == -1) {
            // Perform your action here
        }
    }
});
Dharmesh Baldha
  • 820
  • 6
  • 11
0

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
}
Aram Becker
  • 2,026
  • 1
  • 20
  • 32