简体   繁体   中英

How to make spannable text clickable with Accessibility mode on

I have a problem statement where i need to run my application with Accessibility setting on, to have talk back feedback, but the problem here is when i click on a TextView which have Spannable link in it, then it reads the full text but dose not allow me to click on that Spannable text separately while disabling the accessibility allows to make string multi spannable or link clickable.

here is my code to make String clickable :

SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View textView) {
        startActivity(new Intent(MyActivity.this, NextActivity.class));
    }
    @Override
    public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);

If you are using Android X library you should be able to handle accessibility and clickable spannable strings by:

ViewCompat.enableAccessibleClickableSpanSupport(yourView);

Also make sure you have the latest dependency:

com.android.support:appcompat-v7:28.0.0

It should work back to API 19.

Note: To enable Android X library go to your gradle.properties and add these lines:

android.useAndroidX = true
android.enableJetifier = true

I'm afraid there is no way in android to implement that (I had the same issue for months). the only way is using the local context menu. Looks like Talkback is trying to make the ADA users to get use to the menus using there gestures, which will fix too many issue in our dev side. There might be another way, which is creating a WebView and then add html elements which will handle everything, BUT this will be bad for the app performance :(

As mentioned here: Clickable links (Google support)

you have to access local context menu to activate any clickable span by Swiping up and then right , and then click on Links submenu.

Hope this helps :)

Nothing to do.Just you click the Spannable link with two finger on the screen, and one of them must be on the Spannable link. Try some times!!!

Try this one, worked for me. An alternate working solution.

private void setUpBottomSheetAccessibility() {
    ViewCompat.setAccessibilityDelegate(view, new AccessibilityDelegateCompat() {

        @Override
        public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
            if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_LONG_CLICKED) {
                //Put your work to be done on double click;
            }
            super.onPopulateAccessibilityEvent(host, event);
        }
    });
}

Above code view is your Textview or any view. And this event work on double click.

Working fine at my end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM