AccessibilityRoleDescriptionSupportActivity.java revision b31c3281d870e9abb673db239234d580dcc4feff
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 androidx.core.view.AccessibilityDelegateCompat; 22import androidx.core.view.ViewCompat; 23import androidx.core.view.accessibility.AccessibilityNodeInfoCompat; 24import android.view.View; 25import android.widget.Button; 26import android.widget.TextView; 27 28import com.example.android.supportv4.R; 29 30/** 31 * This class demonstrates how to use the support library to set custom 32 * role descriptions on your views. This functionality is supported in the 33 * support-v4 library on devices running KitKat (API 19) or later. 34 */ 35public class AccessibilityRoleDescriptionSupportActivity extends Activity { 36 37 /** 38 * {@inheritDoc} 39 */ 40 @Override 41 public void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.accessibility_roledescription); 44 45 TextView firstTextView = findViewById(R.id.text_view_1); 46 String roleDescriptionTextView = getString(R.string.accessibility_roledescription_role); 47 ViewCompat.setAccessibilityDelegate(firstTextView, 48 new RoleDescriptionAccessibilityDelegate(roleDescriptionTextView)); 49 50 TextView heading1 = findViewById(R.id.text_heading_1); 51 String roleDescriptionHeading1 = getString(R.string.accessibility_roledescription_h1_role); 52 ViewCompat.setAccessibilityDelegate(heading1, 53 new RoleDescriptionAccessibilityDelegate(roleDescriptionHeading1)); 54 55 TextView heading2 = findViewById(R.id.text_heading_2); 56 String roleDescriptionHeading2 = getString(R.string.accessibility_roledescription_h2_role); 57 ViewCompat.setAccessibilityDelegate(heading2, 58 new RoleDescriptionAccessibilityDelegate(roleDescriptionHeading2)); 59 60 TextView heading3 = findViewById(R.id.text_heading_3); 61 String roleDescriptionHeading3 = getString(R.string.accessibility_roledescription_h3_role); 62 ViewCompat.setAccessibilityDelegate(heading3, 63 new RoleDescriptionAccessibilityDelegate(roleDescriptionHeading3)); 64 65 // This is an example of an <strong>incorrect</strong> use of the role description. 66 // You should not set the role description for standard widgets in your own code. 67 Button button = findViewById(R.id.button); 68 String roleDescriptionButton = 69 getString(R.string.accessibility_roledescription_button_role); 70 ViewCompat.setAccessibilityDelegate(button, 71 new RoleDescriptionAccessibilityDelegate(roleDescriptionButton)); 72 } 73 74 /** 75 * This class subclasses AccessibilityDelegateCompat to modify a view's role description. 76 * You can either override View.onPopulateAccessibilityEvent (API 14+) or use an accessibility 77 * delegate to set the role description. Using an accessibility delegate provides pre-ICS 78 * compatibility, and helps to organize your accessibility-related code. 79 */ 80 private static class RoleDescriptionAccessibilityDelegate extends AccessibilityDelegateCompat { 81 private final String mRoleDescription; 82 83 public RoleDescriptionAccessibilityDelegate(String roleDescription) { 84 mRoleDescription = roleDescription; 85 } 86 87 @Override 88 public void onInitializeAccessibilityNodeInfo(View host, 89 AccessibilityNodeInfoCompat info) { 90 super.onInitializeAccessibilityNodeInfo(host, info); 91 // This call will succeed on all platforms, but it will only set the role description 92 // on devices running KitKat (API 19) or later. On older platforms the method call 93 // will succeed but do nothing. 94 info.setRoleDescription(mRoleDescription); 95 } 96 } 97 98} 99