Skip to content

Commit a3b5440

Browse files
leticiarossidrchen
authored andcommitted
[Catalog][TextInputLayout] Added update button to customizable text fields.
Also updated text to make things less confusing. PiperOrigin-RevId: 407415749
1 parent e9ec82f commit a3b5440

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

catalog/java/io/material/catalog/textfield/TextFieldControllableDemoFragment.java

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
6464
labelEditText.setOnEditorActionListener(
6565
(v, actionId, event) -> {
6666
if (actionId == EditorInfo.IME_ACTION_DONE) {
67-
if (!checkTextInputIsNull(labelTextField)) {
67+
if (!checkTextInputIsNull(labelTextField, /* showError= */ true)) {
6868
setAllTextFieldsLabel(String.valueOf(labelEditText.getText()));
6969
showToast(R.string.cat_textfield_toast_label_text);
7070
}
@@ -79,19 +79,9 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
7979
inputErrorEditText.setOnEditorActionListener(
8080
(v, actionId, event) -> {
8181
if (actionId == EditorInfo.IME_ACTION_DONE) {
82-
if (!checkTextInputIsNull(textInputError)) {
83-
errorText = String.valueOf(inputErrorEditText.getText());
82+
if (!checkTextInputIsNull(textInputError, /* showError= */ true)) {
83+
updateErrorText(String.valueOf(inputErrorEditText.getText()), toggleErrorButton);
8484
showToast(R.string.cat_textfield_toast_error_text);
85-
// if error already showing, call setError again to update its text.
86-
if (toggleErrorButton
87-
.getText()
88-
.toString()
89-
.equals(getResources().getString(R.string.cat_textfield_hide_error_text))) {
90-
for (TextInputLayout textfield : textfields) {
91-
setErrorIconClickListeners(textfield);
92-
textfield.setError(errorText);
93-
}
94-
}
9585
}
9686
return true;
9787
}
@@ -104,7 +94,7 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
10494
helperTextEditText.setOnEditorActionListener(
10595
(v, actionId, event) -> {
10696
if (actionId == EditorInfo.IME_ACTION_DONE) {
107-
if (!checkTextInputIsNull(helperTextTextField)) {
97+
if (!checkTextInputIsNull(helperTextTextField, /* showError= */ true)) {
10898
setAllTextFieldsHelperText(String.valueOf(helperTextEditText.getText()));
10999
showToast(R.string.cat_textfield_toast_helper_text);
110100
}
@@ -119,7 +109,7 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
119109
placeholderEditText.setOnEditorActionListener(
120110
(v, actionId, event) -> {
121111
if (actionId == EditorInfo.IME_ACTION_DONE) {
122-
if (!checkTextInputIsNull(placeholderTextField)) {
112+
if (!checkTextInputIsNull(placeholderTextField, /* showError= */ true)) {
123113
setAllTextFieldsPlaceholder(String.valueOf(placeholderEditText.getText()));
124114
showToast(R.string.cat_textfield_toast_placeholder_text);
125115
}
@@ -134,7 +124,7 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
134124
counterEditText.setOnEditorActionListener(
135125
(v, actionId, event) -> {
136126
if (actionId == EditorInfo.IME_ACTION_DONE) {
137-
if (!checkTextInputIsNull(counterMaxTextField)) {
127+
if (!checkTextInputIsNull(counterMaxTextField, /* showError= */ true)) {
138128
int length = Integer.parseInt(counterEditText.getText().toString());
139129
setAllTextFieldsCounterMax(length);
140130
showToast(R.string.cat_textfield_toast_counter_text);
@@ -144,6 +134,37 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
144134
return false;
145135
});
146136

137+
// Initialize button for updating all customizable fields.
138+
Button updateButton = view.findViewById(R.id.button_update);
139+
updateButton.setOnClickListener(
140+
v -> {
141+
boolean updated = false;
142+
if (!checkTextInputIsNull(labelTextField, /* showError= */ false)) {
143+
setAllTextFieldsLabel(String.valueOf(labelEditText.getText()));
144+
updated = true;
145+
}
146+
if (!checkTextInputIsNull(textInputError, /* showError= */ false)) {
147+
updateErrorText(String.valueOf(inputErrorEditText.getText()), toggleErrorButton);
148+
updated = true;
149+
}
150+
if (!checkTextInputIsNull(helperTextTextField, /* showError= */ false)) {
151+
setAllTextFieldsHelperText(String.valueOf(helperTextEditText.getText()));
152+
updated = true;
153+
}
154+
if (!checkTextInputIsNull(counterMaxTextField, /* showError= */ false)) {
155+
int length = Integer.parseInt(counterEditText.getText().toString());
156+
setAllTextFieldsCounterMax(length);
157+
updated = true;
158+
}
159+
if (!checkTextInputIsNull(placeholderTextField, /* showError= */ false)) {
160+
setAllTextFieldsPlaceholder(String.valueOf(placeholderEditText.getText()));
161+
updated = true;
162+
}
163+
if (updated) {
164+
showToast(R.string.cat_textfield_toast_update_button);
165+
}
166+
});
167+
147168
// Initializing switch to toggle between disabling or enabling text fields.
148169
SwitchMaterial enabledSwitch = view.findViewById(R.id.cat_textfield_enabled_switch);
149170
enabledSwitch.setOnCheckedChangeListener(
@@ -177,6 +198,20 @@ private void setAllTextFieldsError(String error) {
177198
}
178199
}
179200

201+
private void updateErrorText(String errorText, Button toggleErrorButton) {
202+
this.errorText = errorText;
203+
// if error already showing, call setError again to update its text.
204+
if (toggleErrorButton
205+
.getText()
206+
.toString()
207+
.equals(getResources().getString(R.string.cat_textfield_hide_error_text))) {
208+
for (TextInputLayout textfield : textfields) {
209+
setErrorIconClickListeners(textfield);
210+
textfield.setError(errorText);
211+
}
212+
}
213+
}
214+
180215
private void setErrorIconClickListeners(TextInputLayout textfield) {
181216
textfield.setErrorIconOnClickListener(
182217
v -> showToast(R.string.cat_textfield_toast_error_icon_click));
@@ -205,11 +240,13 @@ private void setAllTextFieldsCounterMax(int length) {
205240
}
206241
}
207242

208-
private boolean checkTextInputIsNull(TextInputLayout textInputLayout) {
243+
private boolean checkTextInputIsNull(TextInputLayout textInputLayout, boolean showError) {
209244
if (textInputLayout.getEditText().getText() == null
210245
|| textInputLayout.getEditText().length() == 0) {
211-
textInputLayout.setError(
212-
getResources().getString(R.string.cat_textfield_null_input_error_text));
246+
if (showError) {
247+
textInputLayout.setError(
248+
getResources().getString(R.string.cat_textfield_null_input_error_text));
249+
}
213250
return true;
214251
}
215252
textInputLayout.setError(null);

catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_controls.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@
146146
android:inputType="text"
147147
android:imeOptions="actionDone"/>
148148
</com.google.android.material.textfield.TextInputLayout>
149+
150+
<Button
151+
android:id="@+id/button_update"
152+
android:layout_width="wrap_content"
153+
android:layout_height="wrap_content"
154+
android:layout_marginTop="@dimen/cat_textfield_standard_spacing"
155+
android:layout_gravity="center_horizontal"
156+
android:text="@string/cat_textfield_update_button"/>
149157
</LinearLayout>
150158
</LinearLayout>
151159
</ScrollView>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969
<string name="cat_textfield_customize_section_title" translatable="false">Customize text fields</string>
7070
<string name="cat_textfield_customize_section_description" translatable="false">
71-
Enter values below to customize the text fields above. Press \"done\" to update each field.
71+
Enter values below to customize the text fields above. Press the keyboard \"done\" button to update each field, or press the update button at the bottom to update all fields at once.
7272
</string>
7373
<string name="cat_textfield_label" translatable="false">Label</string>
7474
<string name="cat_textfield_password" translatable="false">Password</string>
@@ -94,10 +94,12 @@
9494

9595
<string name="cat_textfield_show_leading_icon" translatable="false">Show leading icon</string>
9696
<string name="cat_textfield_hide_leading_icon" translatable="false">Hide leading icon</string>
97+
<string name="cat_textfield_update_button" description="Button to update customizable fields.[CHAR_LIMIT=NONE]">Update</string>
9798

9899
<string name="cat_textfield_label_enabled" description="Label on a switch to enable text fields.[CHAR_LIMIT=10]">Text fields enabled</string>
99100
<string name="cat_textfield_label_disabled" translatable="false">Text fields disabled</string>
100101

102+
<string name="cat_textfield_toast_update_button" description="Announcement for when update button is clicked.[CHAR_LIMIT=NONE]">Text fields updated.</string>
101103
<string name="cat_textfield_toast_label_text" description="Announcement for when label is updated.[CHAR_LIMIT=NONE]">Labels updated.</string>
102104
<string name="cat_textfield_toast_error_text" description="Announcement for when error text is updated.[CHAR_LIMIT=NONE]">Error messages updated.</string>
103105
<string name="cat_textfield_toast_helper_text" description="Announcement for when helper text is updated.[CHAR_LIMIT=NONE]">Helper texts updated.</string>

0 commit comments

Comments
 (0)