Search code examples
javaandroidaccessibilitytalkbackclickablespan

Not works clickable spans with talkback 12.2 on android 11 (api 30)


I have a custom view,which contains clickable span. But if i go to talkback menue,links and press on my link,nothing happens. But i expect to see notification. Whether it's issue,which should be reported to google? Thanks everybody for any help.

myview.java

package ru.alexandrkozlovskiy.test;

import android.content.Context;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

class myview extends View {
    public myview(Context context) {
        super(context);
        setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    public myview(Context context, AttributeSet attrs) {
        super(context, attrs);
        setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    public myview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    public myview(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    @Override
    public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
        super.onInitializeAccessibilityNodeInfo(info);
        SpannableStringBuilder sb= new SpannableStringBuilder("Test of clickable span");
        sb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View v) {
                try {
                    Toast.makeText(getContext(),"Test of clickable span",1).show();
                }
                catch (Exception e) {
                    info.setContentDescription(e.toString());
                    setContentDescription(e.toString());
                }
            }
        }, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        info.setText(sb);
    }
}

MainActivity.java

package ru.alexandrkozlovskiy.test;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityNodeProvider;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
public myview tv;
public LinearLayout c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
tv = findViewById(R.id.tv);
c=findViewById(R.id.c);
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/c"
    tools:context=".MainActivity">
<ru.alexandrkozlovskiy.test.myview
        android:layout_width="match_parent"
        android:layout_height="match_parent"
android:id="@+id/tv"/>
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ru.alexandrkozlovskiy.test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Solution

  • @Ma3x I dont know why,but we must have special variable for our text. So,for myview we can simple save our result in variable,and set it as text to accessibilityNodeInfo.

    package ru.alexandrkozlovskiy.test;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.text.SpannableStringBuilder;
    import android.text.Spanned;
    import android.text.style.ClickableSpan;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.accessibility.AccessibilityEvent;
    import android.view.accessibility.AccessibilityNodeInfo;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import androidx.annotation.Nullable;
    
    class myview extends View {
        SpannableStringBuilder sb= new SpannableStringBuilder("Test of clickable span");
        public myview(Context context) {
            this(context,null);
        }
    
        public myview(Context context, AttributeSet attrs) {
            this(context, attrs,0);
        }
    
        public myview(Context context, AttributeSet attrs, int defStyleAttr) {
            this(context, attrs, defStyleAttr,0);
        }
    
        public myview(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
            sb.setSpan(new ClickableSpan() {
                @Override
                public void onClick(View v) {
                    try {
                        Toast.makeText(getContext(),"Test of clickable span",1).show();
                    }
                    catch (Exception e) {
                        setContentDescription(e.toString());
                    }
                }
            }, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    
        @Override
        public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
            super.onInitializeAccessibilityNodeInfo(info);
            info.setText(sb);
        }
    }
    

    I hope someone explain to me,why if i recreate my variable,as it was before,talkback not click on this links. note: Please not try see on screen,but use talkback and swipe to this view.