1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.example.android.supportv4.accessibility; 18 19import android.app.Activity; 20import android.os.Bundle; 21import android.view.View; 22import android.widget.Button; 23import android.widget.TextView; 24 25import androidx.core.view.AccessibilityDelegateCompat; 26import androidx.core.view.ViewCompat; 27import androidx.core.view.accessibility.AccessibilityNodeInfoCompat; 28 29import com.example.android.supportv4.R; 30 31/** 32 * This class demonstrates how to use the support library to set custom 33 * role descriptions on your views. This functionality is supported in the 34 * support-v4 library on devices running KitKat (API 19) or later. 35 */ 36public class AccessibilityRoleDescriptionSupportActivity extends Activity { 37 38 /** 39 * {@inheritDoc} 40 */ 41 @Override 42 public void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.accessibility_roledescription); 45 46 TextView firstTextView = findViewById(R.id.text_view_1); 47 String roleDescriptionTextView = getString(R.string.accessibility_roledescription_role); 48 ViewCompat.setAccessibilityDelegate(firstTextView, 49 new RoleDescriptionAccessibilityDelegate(roleDescriptionTextView)); 50 51 TextView heading1 = findViewById(R.id.text_heading_1); 52 String roleDescriptionHeading1 = getString(R.string.accessibility_roledescription_h1_role); 53 ViewCompat.setAccessibilityDelegate(heading1, 54 new RoleDescriptionAccessibilityDelegate(roleDescriptionHeading1)); 55 56 TextView heading2 = findViewById(R.id.text_heading_2); 57 String roleDescriptionHeading2 = getString(R.string.accessibility_roledescription_h2_role); 58 ViewCompat.setAccessibilityDelegate(heading2, 59 new RoleDescriptionAccessibilityDelegate(roleDescriptionHeading2)); 60 61 TextView heading3 = findViewById(R.id.text_heading_3); 62 String roleDescriptionHeading3 = getString(R.string.accessibility_roledescription_h3_role); 63 ViewCompat.setAccessibilityDelegate(heading3, 64 new RoleDescriptionAccessibilityDelegate(roleDescriptionHeading3)); 65 66 // This is an example of an <strong>incorrect</strong> use of the role description. 67 // You should not set the role description for standard widgets in your own code. 68 Button button = findViewById(R.id.button); 69 String roleDescriptionButton = 70 getString(R.string.accessibility_roledescription_button_role); 71 ViewCompat.setAccessibilityDelegate(button, 72 new RoleDescriptionAccessibilityDelegate(roleDescriptionButton)); 73 } 74 75 /** 76 * This class subclasses AccessibilityDelegateCompat to modify a view's role description. 77 * You can either override View.onPopulateAccessibilityEvent (API 14+) or use an accessibility 78 * delegate to set the role description. Using an accessibility delegate provides pre-ICS 79 * compatibility, and helps to organize your accessibility-related code. 80 */ 81 private static class RoleDescriptionAccessibilityDelegate extends AccessibilityDelegateCompat { 82 private final String mRoleDescription; 83 84 public RoleDescriptionAccessibilityDelegate(String roleDescription) { 85 mRoleDescription = roleDescription; 86 } 87 88 @Override 89 public void onInitializeAccessibilityNodeInfo(View host, 90 AccessibilityNodeInfoCompat info) { 91 super.onInitializeAccessibilityNodeInfo(host, info); 92 // This call will succeed on all platforms, but it will only set the role description 93 // on devices running KitKat (API 19) or later. On older platforms the method call 94 // will succeed but do nothing. 95 info.setRoleDescription(mRoleDescription); 96 } 97 } 98 99} 100