7

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);
    }
}
Jonik
  • 80,077
  • 70
  • 264
  • 372
Tako
  • 3,364
  • 2
  • 14
  • 21

1 Answers1

12

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) {

            }
        });
Abhishek V
  • 12,488
  • 6
  • 51
  • 63
  • Thanks for the answer, it still have two issues: 1. Talkback doesn't focus on the error message (not a big deal) 2. When I type in the editText after showing the error one, talkback reads the error message again - after the visual error already disappeared. Any idea how to deal with those issues? – Tako Dec 12 '16 at 07:59
  • 1) Not sure how to fix that 2) When exactly are you calling announceForAccessibility? Can you post that code? – Abhishek V Dec 12 '16 at 08:18
  • I just added the announceForAccessibility() to my custom EditText's setError (see update to question) – Tako Dec 12 '16 at 09:08
  • 1
    Yap, that fixed it. Thanks! – Tako Dec 13 '16 at 06:37
  • Hi Abhisek, I am using an EditText for Zipcode purpose. after entered zipcode:4587. It reading as four thousand eight seven like that, But, I want to read as Four Five Eight Seven. any update for your code on the above? any suggestion. – harikrishnan Sep 07 '18 at 04:06
  • @harikrishnan I guess you can programmatically append space between each characters. It should read it out as individual numbers – Abhishek V Sep 07 '18 at 04:23
  • thank you for the reply. I tried it in Textview, which is reading as expected. the same for EditText also. but, its not reading as expected. If possible , please try once for Edittext with Zip code numeric and let us know, if possible. – harikrishnan Sep 07 '18 at 07:53