Rakesh Sharma
Rakesh Sharma

Reputation: 41

Customize Android Talkback in Toast

I've checked with all default Toast message via Android TalkBack. Default Android Talkback behavior is that it reads all contents(non stop) in Toast. Is there any way I can customize it according to my need. For example :

Toast.makeText(getApplicationContext(), "Hello world! Welcome to Android. This is Stackoverflow.", Toast.LENGTH_SHORT).show();

When Toast appears, it reads automatically "Hello world! Welcome to Android. This is Stackoverflow." But I want to control it, like it should read only "Hello world" or "Welcome to Android" or "This is Stackoverflow" etc.

Upvotes: 4

Views: 2127

Answers (1)

ataulm
ataulm

Reputation: 15334

I can't think of a reason to do this - typically, you want to have some kind of parity between the information given to your users, regardless of the way they consume the information.

If you're just looking for TalkBack to read aloud text, perhaps you could use the View.announceForAccessibility API directly. That said...


Yes, it's possible to have something different read aloud by the spoken feedback accessibility service than that which is rendered visibly.

Toast toast = Toast.makeText(this, "visible text", Toast.LENGTH_SHORT);
View.AccessibilityDelegate delegate = new View.AccessibilityDelegate() {
    @Override
    public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
        super.onPopulateAccessibilityEvent(host, event);
        event.setContentDescription("audible text");
    }
};
((ViewGroup) toast.getView()).getChildAt(0).setAccessibilityDelegate(delegate);
toast.show();

Note the super hacky way to get a reference to the TextView. This is dangerous because it could break on different implementations of Android.

You can instead set your own view on the Toast directly:

Toast toast = new Toast(this);
TextView messageView = new TextView(this);
messageView.setText("visible text");
View.AccessibilityDelegate delegate = new View.AccessibilityDelegate() {
    @Override
    public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
        super.onPopulateAccessibilityEvent(host, event);
        event.setContentDescription("audible text");
    }
};
messageView.setAccessibilityDelegate(delegate);
toast.setView(messageView);
toast.show();

Upvotes: 3

Related Questions