File tree Expand file tree Collapse file tree 4 files changed +40
-0
lines changed
javatests/com/google/android/material/datepicker
java/com/google/android/material/datepicker Expand file tree Collapse file tree 4 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -235,6 +235,11 @@ public View onCreateTextInputView(
235
235
if (selectedStartItem != null ) {
236
236
startEditText .setText (format .format (selectedStartItem ));
237
237
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
+ }
238
243
}
239
244
if (selectedEndItem != null ) {
240
245
endEditText .setText (format .format (selectedEndItem ));
Original file line number Diff line number Diff line change @@ -135,6 +135,11 @@ public View onCreateTextInputView(
135
135
dateTextInput .setPlaceholderText (formatHint );
136
136
if (selectedItem != null ) {
137
137
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
+ }
138
143
}
139
144
140
145
dateEditText .addTextChangedListener (
Original file line number Diff line number Diff line change @@ -599,6 +599,21 @@ public void focusAndShowKeyboardAtStartup() {
599
599
assertThat (shadowIMM .isSoftInputVisible ()).isTrue ();
600
600
}
601
601
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
+
602
617
private View getRootView () {
603
618
return rangeDateSelector .onCreateTextInputView (
604
619
LayoutInflater .from (context ),
Original file line number Diff line number Diff line change @@ -345,6 +345,21 @@ public void focusAndShowKeyboardAtStartup() {
345
345
assertThat (shadowIMM .isSoftInputVisible ()).isTrue ();
346
346
}
347
347
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
+
348
363
private View getRootView () {
349
364
return singleDateSelector .onCreateTextInputView (
350
365
LayoutInflater .from (context ),
You can’t perform that action at this time.
0 commit comments