blob: e251ae03e837d31dec9f308394c56a616a75ddbf [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?>
Copyright 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
Sample showing how to make an accessible custom view that extends
a layout.
<template src="base-build" />
Sample demonstrating how to make a simple custom view accessible.
Creates a simple `CheckableFrameLayout` that has the accessibility
profile of builtin that implements the `Checkable` interface (e.g.
This sample implements a single custom view, a
`CheckableFrameLayout`, which implements the `Checkable` interface
and maintains a checked state.
This sample uses the custom view as a container for two overlayed
`<![CDATA[<ImageView>]]>`s. The first `<![CDATA[<ImageView>]]>` is
always displayed, while the second `<![CDATA[<ImageView>]]>`'s
display status toggles with the checked state of it's parent (the
`CheckableLayout`). This second `<![CDATA[<ImageView>]]>` uses
`android:duplicateParentState` (see `activity_main.xml`) and a
drawable that is tied to `android:state_checked` (see
`drawable/ic_checkable_item.xml`). This drawable is displayed only
when the parent is in a checked state.
This sample provides a basic introduction to Android accessibility
by showing how to make a custom view behave similar to a builtin
widget like a `Checkbox`.
Developers should focus on two methods that are overridden from the
parent class:
`onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)` and
`onInitializeAccessibilityEvent(AccessibilityEvent event)`.
The `onInitializeAccessibilityNodeInfo()` method provides
accessibility services with information about the state of this
view. We override this method to tell an accessibility service that
this view is checkable, and we set the intitial checked state of the
The `onInitializeAccessibilityEvent()` method is use by the
system to obtain additional information about the state of the view
as its state changes.
With this information, an accessibility service has what it needs to
allow a user to meaningfully discover the view and know its state.