Skip to content

Commit d148048

Browse files
imhappikendrickumstattd
authored andcommitted
[NavigationRail][a11y] Add a11y announcements for when nav rail is expanded or collapsed
PiperOrigin-RevId: 689858577
1 parent 08c23dc commit d148048

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

catalog/java/io/material/catalog/navigationrail/NavigationRailSubMenuDemoFragment.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ public View onCreateDemoView(
7171

7272
ImageView button =
7373
navigationRailView.getHeaderView().findViewById(R.id.cat_navigation_rail_expand_button);
74+
button.setContentDescription(getResources().getString(R.string.cat_navigation_rail_expand_button_description));
7475
button.setOnClickListener(
7576
v -> {
7677
if (efab.isExtended()) {
7778
efab.shrink();
7879
navigationRailView.collapse();
80+
button.setContentDescription(getResources().getString(R.string.cat_navigation_rail_expand_button_description));
7981
} else {
8082
efab.extend();
8183
navigationRailView.expand();
84+
button.setContentDescription(getResources().getString(R.string.cat_navigation_rail_collapse_button_description));
8285
}
8386
});
8487
return view;

catalog/java/io/material/catalog/navigationrail/res/layout/cat_navigation_rail_efab_header_view.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
android:layout_width="56dp"
2929
android:layout_height="56dp"
3030
android:src="@drawable/ic_drawer_menu_24px"
31-
android:contentDescription="@null"
31+
android:contentDescription=
32+
"@string/cat_navigation_rail_expand_button_description"
3233
android:scaleType="center" />
3334
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
3435
android:id="@+id/cat_navigation_rail_efab"

catalog/java/io/material/catalog/navigationrail/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<string name="cat_navigation_rail_efab_text" description="The text on the button to expand the navigation rail [CHAR_LIMIT=NONE]">Label</string>
130130
<string name="cat_navigation_rail_center_text" description="The text in the content centered in the remaining space beside the navigation rail [CHAR_LIMIT=NONE]">center</string>
131131
<string name="cat_navigation_rail_efab_message" description="The message to display when the extended floating action button is clicked [CHAR_LIMIT=NONE]">Button clicked</string>
132-
<string name="cat_navigation_rail_expand_collapse_button_description" description="The content description for the button that expands and collapses the navigation rail [CHAR_LIMIT=NONE]">Button to expand or collapse the navigation rail</string>
132+
<string name="cat_navigation_rail_expand_button_description" description="The content description for the button that expands the navigation rail [CHAR_LIMIT=NONE]">Expand navigation rail</string>
133+
<string name="cat_navigation_rail_collapse_button_description" description="The content description for the button that collapses the navigation rail [CHAR_LIMIT=NONE]">Collapse navigation rail</string>
133134

134135
</resources>

lib/java/com/google/android/material/navigationrail/NavigationRailView.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,11 @@ private void setExpanded(boolean expanded) {
374374

375375
/** Expand the navigation rail. */
376376
public void expand() {
377+
if (expanded) {
378+
return;
379+
}
377380
setExpanded(true);
381+
announceForAccessibility(getResources().getString(R.string.nav_rail_expanded_a11y_label));
378382
}
379383

380384
/** Returns whether or not the navigation rail is currently expanded. **/
@@ -384,7 +388,11 @@ public boolean isExpanded() {
384388

385389
/** Collapse the navigation rail. */
386390
public void collapse() {
391+
if (!expanded) {
392+
return;
393+
}
387394
setExpanded(false);
395+
announceForAccessibility(getResources().getString(R.string.nav_rail_collapsed_a11y_label));
388396
}
389397

390398
private void applyWindowInsets() {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2022 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<resources>
18+
<string name="nav_rail_expanded_a11y_label" description="Screen reader announcement for when navigation rail is expanded [CHAR LIMIT=NONE]">
19+
Expanded
20+
</string>
21+
<string name="nav_rail_collapsed_a11y_label" description="Screen reader announcement for when navigation rail is collapsed [CHAR LIMIT=NONE]">
22+
Collapsed
23+
</string>
24+
</resources>

0 commit comments

Comments
 (0)