Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make TalkBack read TextView error message automatically?

I'm currently making my app accessible and I'm having problem with my EditTexts:

In every EditText, the user's input is being validated at some point (e.g. after pressing a button) and if the input is invalid, I show an error using editText.setError("message"). The problem is that if TalkBack is on, it will not automatically focus and read the error. Also, since I can't get the error's view, I can't ask TalkBack to focus it via sendAccessibilityEvent.

I would appreciate any ideas on how to solve this issue while still using editText.setError().

Edit 1 Added code for @Abhishek V solution:

public class BaseEditText extends EditText {

    ...
    ...

    @Override
    public void setError(CharSequence error) {
        super.setError(error);
        announceForAccessibility(error);
    }
}
like image 370
Tako Avatar asked Nov 20 '16 15:11

Tako


1 Answers

You can explicitly read out the error message through announceForAccessibility("mesage") function provided by View

editText.setError("message")
editText.announceForAccessibility("message");

Please note that this function was added in API level 16.

update 1: Set the error message to null when the text is changed in EditText to prevent reading error message again and again.

 editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                editText.setError(null);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
like image 187
Abhishek V Avatar answered Nov 13 '22 13:11

Abhishek V