Skip to content

Commit e69709f

Browse files
Material Design Teamdrchen
authored andcommitted
[TimePicker][A11y] Add tooltip to MaterialTimePicker mode toggle button
Refactored the logic for retrieving the toggle button's icon and content description into separate methods and added a tooltip to the mode toggle button. PiperOrigin-RevId: 797722507
1 parent 89ad742 commit e69709f

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

lib/java/com/google/android/material/timepicker/MaterialTimePicker.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import android.content.res.TypedArray;
3131
import android.os.Bundle;
3232
import androidx.fragment.app.DialogFragment;
33+
import androidx.appcompat.widget.TooltipCompat;
3334
import android.text.TextUtils;
34-
import android.util.Pair;
3535
import android.util.TypedValue;
3636
import android.view.LayoutInflater;
3737
import android.view.View;
@@ -398,9 +398,12 @@ private void updateInputMode(MaterialButton modeButton) {
398398
initializeOrRetrieveActivePresenterForMode(inputMode, timePickerView, textInputStub);
399399
activePresenter.show();
400400
activePresenter.invalidate();
401-
Pair<Integer, Integer> buttonData = dataForMode(inputMode);
402-
modeButton.setIconResource(buttonData.first);
403-
modeButton.setContentDescription(getResources().getString(buttonData.second));
401+
ModeButtonData modeButtonData = getModeButtonData(inputMode);
402+
modeButton.setIconResource(modeButtonData.iconResId);
403+
modeButton.setContentDescription(
404+
getResources().getString(modeButtonData.contentDescriptionResId));
405+
TooltipCompat.setTooltipText(
406+
modeButton, getResources().getString(modeButtonData.tooltipTextResId));
404407
modeButton.sendAccessibilityEvent(
405408
AccessibilityEventCompat.CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION);
406409
}
@@ -432,15 +435,21 @@ private TimePickerPresenter initializeOrRetrieveActivePresenterForMode(
432435
return timePickerTextInputPresenter;
433436
}
434437

435-
private Pair<Integer, Integer> dataForMode(@InputMode int mode) {
438+
private ModeButtonData getModeButtonData(@InputMode int mode) {
436439
switch (mode) {
437440
case INPUT_MODE_KEYBOARD:
438-
return new Pair<>(clockIcon, R.string.material_timepicker_clock_mode_description);
441+
return new ModeButtonData(
442+
clockIcon,
443+
R.string.material_timepicker_clock_mode_description,
444+
R.string.material_timepicker_clock_mode_tooltip);
439445
case INPUT_MODE_CLOCK:
440-
return new Pair<>(keyboardIcon, R.string.material_timepicker_text_input_mode_description);
446+
return new ModeButtonData(
447+
keyboardIcon,
448+
R.string.material_timepicker_text_input_mode_description,
449+
R.string.material_timepicker_text_input_mode_tooltip);
450+
default:
451+
throw new IllegalArgumentException("no button data for mode: " + mode);
441452
}
442-
443-
throw new IllegalArgumentException("no icon for mode: " + mode);
444453
}
445454

446455
@Nullable
@@ -677,4 +686,19 @@ public MaterialTimePicker build() {
677686
return MaterialTimePicker.newInstance(this);
678687
}
679688
}
689+
690+
private static final class ModeButtonData {
691+
@DrawableRes final int iconResId;
692+
@StringRes final int contentDescriptionResId;
693+
@StringRes final int tooltipTextResId;
694+
695+
ModeButtonData(
696+
@DrawableRes int iconResId,
697+
@StringRes int contentDescriptionResId,
698+
@StringRes int tooltipTextResId) {
699+
this.iconResId = iconResId;
700+
this.contentDescriptionResId = contentDescriptionResId;
701+
this.tooltipTextResId = tooltipTextResId;
702+
}
703+
}
680704
}

lib/java/com/google/android/material/timepicker/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@
3838
<string name="material_timepicker_text_input_mode_description">Switch to text input mode for the time input.</string>
3939
<!-- Accessibility string used for describing the button in time picker that changes the dialog to circular clock mode. [CHAR LIMIT=NONE] -->
4040
<string name="material_timepicker_clock_mode_description">Switch to clock mode for the time input.</string>
41+
42+
<!-- Tooltip text for the button that switches the time picker to text input mode. [CHAR LIMIT=NONE] -->
43+
<string name="material_timepicker_text_input_mode_tooltip">Switch to text input mode</string>
44+
<!-- Tooltip text for the button that switches the time picker to clock mode. [CHAR LIMIT=NONE] -->
45+
<string name="material_timepicker_clock_mode_tooltip">Switch to clock mode</string>
4146
</resources>

0 commit comments

Comments
 (0)