Skip to content

Commit 39dd3a4

Browse files
Material Design Teamimhappi
Material Design Team
authored andcommitted
[DatePicker][A11y] Fix accessibility focus landing on first day of month after year selection
After selecting a year, accessibility focus now correctly returns to the calendar/year selection button instead of jumping to the first day of the month. Also disabled RecyclerView animations when TalkBack is enabled to prevent incorrect date announcements during transitions. PiperOrigin-RevId: 745964584
1 parent 53616ad commit 39dd3a4

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

lib/java/com/google/android/material/datepicker/MaterialCalendar.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import android.view.View;
3434
import android.view.View.OnClickListener;
3535
import android.view.ViewGroup;
36+
import android.view.accessibility.AccessibilityEvent;
37+
import android.view.accessibility.AccessibilityManager;
3638
import android.widget.GridView;
3739
import androidx.annotation.NonNull;
3840
import androidx.annotation.Nullable;
@@ -92,6 +94,8 @@ enum CalendarSelector {
9294
private View monthNext;
9395
private View yearFrame;
9496
private View dayFrame;
97+
private MaterialButton monthDropSelect;
98+
private AccessibilityManager accessibilityManager;
9599

96100
@NonNull
97101
public static <T> MaterialCalendar<T> newInstance(
@@ -149,6 +153,9 @@ public View onCreateView(
149153
calendarStyle = new CalendarStyle(themedContext);
150154
LayoutInflater themedInflater = layoutInflater.cloneInContext(themedContext);
151155

156+
accessibilityManager =
157+
(AccessibilityManager) requireContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
158+
152159
Month earliestMonth = calendarConstraints.getStart();
153160

154161
int layout;
@@ -335,18 +342,23 @@ CalendarConstraints getCalendarConstraints() {
335342
void setCurrentMonth(Month moveTo) {
336343
MonthsPagerAdapter adapter = (MonthsPagerAdapter) recyclerView.getAdapter();
337344
int moveToPosition = adapter.getPosition(moveTo);
338-
int distance = moveToPosition - adapter.getPosition(current);
339-
boolean jump = Math.abs(distance) > SMOOTH_SCROLL_MAX;
340-
boolean isForward = distance > 0;
341-
current = moveTo;
342-
if (jump && isForward) {
343-
recyclerView.scrollToPosition(moveToPosition - SMOOTH_SCROLL_MAX);
344-
postSmoothRecyclerViewScroll(moveToPosition);
345-
} else if (jump) {
346-
recyclerView.scrollToPosition(moveToPosition + SMOOTH_SCROLL_MAX);
347-
postSmoothRecyclerViewScroll(moveToPosition);
345+
if (accessibilityManager != null && accessibilityManager.isEnabled()) {
346+
current = moveTo;
347+
recyclerView.scrollToPosition(moveToPosition);
348348
} else {
349-
postSmoothRecyclerViewScroll(moveToPosition);
349+
int distance = moveToPosition - adapter.getPosition(current);
350+
boolean jump = Math.abs(distance) > SMOOTH_SCROLL_MAX;
351+
boolean isForward = distance > 0;
352+
current = moveTo;
353+
if (jump && isForward) {
354+
recyclerView.scrollToPosition(moveToPosition - SMOOTH_SCROLL_MAX);
355+
postSmoothRecyclerViewScroll(moveToPosition);
356+
} else if (jump) {
357+
recyclerView.scrollToPosition(moveToPosition + SMOOTH_SCROLL_MAX);
358+
postSmoothRecyclerViewScroll(moveToPosition);
359+
} else {
360+
postSmoothRecyclerViewScroll(moveToPosition);
361+
}
350362
}
351363
updateNavigationButtonsEnabled(moveToPosition);
352364
}
@@ -394,6 +406,12 @@ void setSelector(CalendarSelector selector) {
394406
}
395407
}
396408

409+
void sendAccessibilityFocusEventToMonthDropdown() {
410+
if (monthDropSelect != null) {
411+
monthDropSelect.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
412+
}
413+
}
414+
397415
void toggleVisibleSelector() {
398416
if (calendarSelector == CalendarSelector.YEAR) {
399417
setSelector(CalendarSelector.DAY);
@@ -408,7 +426,7 @@ void toggleVisibleSelector() {
408426

409427
private void addActionsToMonthNavigation(
410428
@NonNull final View root, @NonNull final MonthsPagerAdapter monthsPagerAdapter) {
411-
final MaterialButton monthDropSelect = root.findViewById(R.id.month_navigation_fragment_toggle);
429+
monthDropSelect = root.findViewById(R.id.month_navigation_fragment_toggle);
412430
monthDropSelect.setTag(SELECTOR_TOGGLE_TAG);
413431
ViewCompat.setAccessibilityDelegate(
414432
monthDropSelect,

lib/java/com/google/android/material/datepicker/YearGridAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void onClick(View view) {
8686
Month moveTo = calendarConstraints.clamp(current);
8787
materialCalendar.setCurrentMonth(moveTo);
8888
materialCalendar.setSelector(CalendarSelector.DAY);
89+
materialCalendar.sendAccessibilityFocusEventToMonthDropdown();
8990
}
9091
};
9192
}

0 commit comments

Comments
 (0)