12

I am developing an accessible android application where people would be using Explore by Touch and TalkBack accessibility services to use my application.

This is my Android XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/forename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:layout_marginLeft="15dip"
        android:textSize="20sp"
        android:text="@string/forenameText" 
        android:contentDescription="@null"/>

    <EditText
        android:id="@+id/EditTextForename"
        android:layout_width="285dp"
        android:layout_height="65dp"
        android:layout_marginTop="10dip"
        android:layout_marginLeft="15dip"
        android:hint="@string/forenameHint"
        android:inputType="textPersonName"
        android:lines="1"
        android:singleLine="true"
        android:textSize="20sp" >
    </EditText>

</LinearLayout>

strings.xml

<string name="forenameText">Forename</string>
<string name="forenameHint">Enter your forename here</string>

TextView displays the title "Forename" and EditText allows me to enter some details in the form field. The problem I have is that when I drag my finger across the screen by using Explore by Touch, TalkBack picks up the title of the TextView and announces it aloud as "Forename". I want the TextView to only display text and not provide any audible feedback.

I have set contentDescription to @null as you can see from the code above, but TalkBack still announces "Forename" when my finger is located over the TextView.

I have also tried setting contentDescription in my Java class:

TextView forename=(TextView)findViewById(R.id.forename);
forename.setContentDescription("");

However, I still get the same problem. Is there any other way to set contentDescription to null/empty and prevent TalkBack from announcing it aloud?

Java code:

public class MainActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View forename = findViewById(R.id.forename);

        forename.setAccessibilityDelegate(new AccessibilityDelegate() {
          public boolean performAccessibilityAction (View host, int action, Bundle args){
            return true;
          }
        });
    }

}
rds
  • 26,253
  • 19
  • 107
  • 134
Alex
  • 876
  • 6
  • 17
  • 38

6 Answers6

19

Since API 16, Android introduced the following:

android:importantForAccessibility="no"

or

setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO)

Which allows developers to disable talkback all together for certain views.

http://developer.android.com/reference/android/view/View.html

Matt
  • 3,837
  • 26
  • 29
  • 3
    And with revision 22 it's now in the v4 support library. http://developer.android.com/tools/support-library/index.html – Herrbert74 Mar 12 '15 at 13:08
19

For better backwards compatibility:

ViewCompat.setImportantForAccessibility(
        decorativeTextView,
        ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);
bonstio
  • 191
  • 1
  • 4
14

I was trying to do the same today, and was able to set an 'empty' contentDescription on a TextView like so (using a non-breaking whitespace):

decorativeTextView.setContentDescription("\u00A0"); 

now TalkBack doesn't say anything for that TextView.

but I agree with Nick about leaving the label as readable in your case, because hint is only read for empty EditTexts.

Piyush
  • 18,895
  • 5
  • 32
  • 63
salfon
  • 296
  • 2
  • 5
  • This fixed the problem. Thank you so much for your help! Please see my comment under the Nick's post. I have another problem that I'm trying to solve now. – Alex May 11 '13 at 02:39
  • re. that new issue in your comment: i don't get that behavior with your originally posted layout, with some duplicate TextView-EditText pairs to test. do you see the accessibility focus box around an individual TextField you're touching when this happens? I've found that if the container (eg LinearLayout) gets accessibilityfocus instead (the yellowbox), the read-only children would all be read, and are not treated as individually 'touchable' - so you might think you've touched the TextField, but the event (and focus) actually went through to parent... – salfon May 13 '13 at 18:13
  • My "answer" mentions API specific updates, this solution works for older versions of Android as well! Thanks! – Matt Jun 23 '14 at 19:59
  • Helpul answer ! :) – akash89 Apr 08 '16 at 08:09
  • This technique is especially helpful in `RecyclerView`: I've applied this to the `viewHolder.itemView`. Pay attention, view must be focusable. – azizbekian Aug 03 '17 at 10:49
  • Hi Salfon, did you face this scenario... https://stackoverflow.com/questions/52215042/android-accessibility-in-edit-text-box-text-not-reading-properly-expect-after-ta/52231297#52231297 – harikrishnan Sep 10 '18 at 19:27
2

I had a similar problem. I eventually solved it by using the setAccessibilityDelegate method and overriding View.AccessibilityDelegate's performAccessibilityAction method.

try this:

View forename = findViewById(R.id.forename);

forename.setAccessibilityDelegate(new AccessibilityDelegate() {
  public boolean performAccessibilityAction (View host, int action, Bundle args){
    return true;
  }
});
Piyush
  • 18,895
  • 5
  • 32
  • 63
mmaloon
  • 81
  • 1
  • 2
  • Thank you for your reply, mmaloon. I have applied your solution to my program but unfortunately TalkBack still announces "forename" aloud. I've updated my original post with Java code that I currently have. My current workaround is to use ImageViews instead of TextViews as by default they don't provide any audible feedback. – Alex Apr 07 '13 at 21:11
  • I also have another question regarding my EditText form field. I have a "hint" attribute there that reads a string called "forenameHint". That string contains the following text: "Enter your forename here". When I use Explore by Touch and drag my finger across the form field, TalkBack announces "EditBox Enter your forename here". Is there a way to prevent it from announcing "EditBox" before every string in the hint attribute? Or maybe change it to something more user friendly like "Form field: Enter your forename here"? Thanks in advance – Alex Apr 07 '13 at 21:15
2

Why do you not want the TextView to speak "forename"? It is being used as a label for the EditText. Once the user has entered some text the hint "enter your forename here" would no longer be spoken - as far as I know - so the TextView given the user some context for the EditText.

Similarly the announcement of "editbox" gives the user the role of the EditText control. While "form field" might be better it would not be the same behavior as in other apps and in the OS.

Nick Williamson
  • 205
  • 3
  • 11
  • 1
    That's a good point. The reason I don't want the TextView to provide any audible feedback is because I have more than one TextViews and EditText fields in the application. The problem I have is that when I point at one of the TextViews, TalkBack announces all the TextViews aloud. So even if I point at the TextView that is located at the very bottom of the screen, TalkBack announces all the TextViews one by one. I have yet to figure out what causes this problem. – Alex May 11 '13 at 02:37
  • sorry I meant when I drag my finger across a particular TextView on the screen, all the TextViews are announced aloud one by one by the screen reader. – Alex May 13 '13 at 14:21
  • Are you text views collected in a listView or other container where the container has the importantForAccessibility property specifically set to "yes"? – MobA11y Mar 29 '17 at 20:51
0

I had the same problem, and the only thing that worked for me was android:contentDescription=" " (white space).

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • Setting contentDescription to white space used to work for me in the past as well. A few weeks ago TalkBack had released a new update to their software and after that the screen reader started announcing the "text" string of my TextView aloud no matter whether the contentDescription was set to white space or null. I'm sure there is a way to solve the problem by using advanced accessibility services like mmaloon has suggested, but I just don't have the knowledge to implement them yet. – Alex Apr 10 '13 at 22:38