Skip to content

Commit 65e7d8a

Browse files
Material Design Teamkendrickumstattd
authored andcommitted
[DatePicker] Set cursor to end of input text field when input is opened
PiperOrigin-RevId: 766655654
1 parent 167451b commit 65e7d8a

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ public View onCreateTextInputView(
235235
if (selectedStartItem != null) {
236236
startEditText.setText(format.format(selectedStartItem));
237237
proposedTextStart = selectedStartItem;
238+
// Move the cursor to the end of the text field
239+
CharSequence text = startEditText.getText();
240+
if (text != null) {
241+
startEditText.post(() -> startEditText.setSelection(text.length()));
242+
}
238243
}
239244
if (selectedEndItem != null) {
240245
endEditText.setText(format.format(selectedEndItem));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ public View onCreateTextInputView(
135135
dateTextInput.setPlaceholderText(formatHint);
136136
if (selectedItem != null) {
137137
dateEditText.setText(format.format(selectedItem));
138+
// Move the cursor to the end of the text field
139+
CharSequence text = dateEditText.getText();
140+
if (text != null) {
141+
dateEditText.post(() -> dateEditText.setSelection(text.length()));
142+
}
138143
}
139144

140145
dateEditText.addTextChangedListener(

lib/javatests/com/google/android/material/datepicker/RangeDateSelectorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,21 @@ public void focusAndShowKeyboardAtStartup() {
599599
assertThat(shadowIMM.isSoftInputVisible()).isTrue();
600600
}
601601

602+
@Test
603+
public void textField_shouldSetCursorToEndOfText() {
604+
Calendar calendar = UtcDates.getUtcCalendar();
605+
calendar.set(2025, Calendar.FEBRUARY, 1);
606+
rangeDateSelector.setSelection(new Pair<>(calendar.getTimeInMillis(), null));
607+
View root = getRootView();
608+
((ViewGroup) activity.findViewById(android.R.id.content)).addView(root);
609+
TextInputLayout startTextInput = root.findViewById(R.id.mtrl_picker_text_input_range_start);
610+
EditText editText = startTextInput.getEditText();
611+
612+
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
613+
614+
assertThat(editText.getSelectionStart()).isEqualTo(editText.getText().length());
615+
}
616+
602617
private View getRootView() {
603618
return rangeDateSelector.onCreateTextInputView(
604619
LayoutInflater.from(context),

lib/javatests/com/google/android/material/datepicker/SingleDateSelectorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,21 @@ public void focusAndShowKeyboardAtStartup() {
345345
assertThat(shadowIMM.isSoftInputVisible()).isTrue();
346346
}
347347

348+
@Test
349+
public void textField_shouldSetCursorToEndOfText() {
350+
Calendar calendar = UtcDates.getUtcCalendar();
351+
calendar.set(2025, Calendar.FEBRUARY, 1);
352+
singleDateSelector.setSelection(calendar.getTimeInMillis());
353+
View root = getRootView();
354+
((ViewGroup) activity.findViewById(android.R.id.content)).addView(root);
355+
TextInputLayout textInputLayout = root.findViewById(R.id.mtrl_picker_text_input_date);
356+
EditText editText = textInputLayout.getEditText();
357+
358+
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
359+
360+
assertThat(editText.getSelectionStart()).isEqualTo(editText.getText().length());
361+
}
362+
348363
private View getRootView() {
349364
return singleDateSelector.onCreateTextInputView(
350365
LayoutInflater.from(context),

0 commit comments

Comments
 (0)