From the course: Android App Development: Accessibility

Create custom accessibility actions - Android Tutorial

From the course: Android App Development: Accessibility

Start my 1-month free trial

Create custom accessibility actions

- [Instructor] Next, we need to make sure that we provide the actions that these buttons were doing via custom actions. To do that, let's use the ViewCompat library and use the setAccessibilityDelegate. Once again, the setAccessibilityDelegate receives a view and then we can modify the accessibility note info for every single view. We're going to do this for the holder.itemView. This is the container view for our record. Now, we create a new AccessibilityDelegateCompat. Let me hide this pane here so that we have more room. Hit Command + N or Alt + Insert to override methods and let's override the initializeAccessibilityNodeInfo. To add an action, all we need to do is to call the addAction method. This method requires a new accessibility action so let's create it. Let's go now and hit Alt + Enter and choose Add on demand static import. Now we have more room. The accessibility action requires two parameters, an action id and a label. So let's create the action ID first. Go back to our project under the resources folder and values, let's create a new resources value file. And let's call it actions. Here we need to create the ID for every single action. So let's create them. First, we need to make sure that we provide the like and let's give the type as id. Now let's duplicate this line and create the rest of the IDs for the other actions. So comment, favorite, share, for the more options, we don't need to pass it, we need can pass directly what is the action inside of the more options menu. So let's add that. We have the archive, the remove, and the report. Let's delete this final line here. So now we have the action IDs. We're also missing the description. Luckily, we previously created the description. We can use the same content description for them. Now we can pass the action ID. Let's look for the like button first. And now let's give the label which we can use the like description that we previously created on another video. Let's do the same thing for the rest of the actions. First for the comments which receives the commentDescription. Now for the favorite which receive the favoriteDescription. Next for the share, shareDescription. We need for the archive and for the archive we don't have it here in the adapter, so what we can do is use the res.getString(R.string.cards_card_archive_this_post). Notice that Android Studio is complaining about res because we're inside of an inner class and this is part of an outer class. Hit Alt + Enter to make this feud final. Let's duplicate a few more and do the same thing for remove, and let's give the correct string, cards remove this post. And report, cards report. Now the accessibility service will report these additional actions. The only problem is that we don't know what to do once the action is performed. I would also like to mention that we can replace the standard action so let's do that. Let's add an action. Let's create a new accessibility action. And for the ID we can use a preexisting one. So let's use the AccessibilityActionCompat.ACTION_CLICK and let's get the ID. By default, this is double tap to activate. We could give more information and more hints to the user about what will happen when they click it. For example, we could say Double tap to view full post. Currently this is not implemented but it's not part of the scope of this video. I just wanted to show that you can change the default click action. Now that we removed the extra accessibility focus, let's create the equivalent custom accessibility actions.

Contents