Skip to content

Commit

Permalink
[Catalog][TextInputLayout] Added update button to customizable text f…
Browse files Browse the repository at this point in the history
…ields.

Also updated text to make things less confusing.

PiperOrigin-RevId: 407415749
  • Loading branch information
leticiarossi authored and drchen committed Nov 4, 2021
1 parent e9ec82f commit a3b5440
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 20 deletions.
Expand Up @@ -64,7 +64,7 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
labelEditText.setOnEditorActionListener(
(v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
if (!checkTextInputIsNull(labelTextField)) {
if (!checkTextInputIsNull(labelTextField, /* showError= */ true)) {
setAllTextFieldsLabel(String.valueOf(labelEditText.getText()));
showToast(R.string.cat_textfield_toast_label_text);
}
Expand All @@ -79,19 +79,9 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
inputErrorEditText.setOnEditorActionListener(
(v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
if (!checkTextInputIsNull(textInputError)) {
errorText = String.valueOf(inputErrorEditText.getText());
if (!checkTextInputIsNull(textInputError, /* showError= */ true)) {
updateErrorText(String.valueOf(inputErrorEditText.getText()), toggleErrorButton);
showToast(R.string.cat_textfield_toast_error_text);
// if error already showing, call setError again to update its text.
if (toggleErrorButton
.getText()
.toString()
.equals(getResources().getString(R.string.cat_textfield_hide_error_text))) {
for (TextInputLayout textfield : textfields) {
setErrorIconClickListeners(textfield);
textfield.setError(errorText);
}
}
}
return true;
}
Expand All @@ -104,7 +94,7 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
helperTextEditText.setOnEditorActionListener(
(v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
if (!checkTextInputIsNull(helperTextTextField)) {
if (!checkTextInputIsNull(helperTextTextField, /* showError= */ true)) {
setAllTextFieldsHelperText(String.valueOf(helperTextEditText.getText()));
showToast(R.string.cat_textfield_toast_helper_text);
}
Expand All @@ -119,7 +109,7 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
placeholderEditText.setOnEditorActionListener(
(v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
if (!checkTextInputIsNull(placeholderTextField)) {
if (!checkTextInputIsNull(placeholderTextField, /* showError= */ true)) {
setAllTextFieldsPlaceholder(String.valueOf(placeholderEditText.getText()));
showToast(R.string.cat_textfield_toast_placeholder_text);
}
Expand All @@ -134,7 +124,7 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
counterEditText.setOnEditorActionListener(
(v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
if (!checkTextInputIsNull(counterMaxTextField)) {
if (!checkTextInputIsNull(counterMaxTextField, /* showError= */ true)) {
int length = Integer.parseInt(counterEditText.getText().toString());
setAllTextFieldsCounterMax(length);
showToast(R.string.cat_textfield_toast_counter_text);
Expand All @@ -144,6 +134,37 @@ public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view)
return false;
});

// Initialize button for updating all customizable fields.
Button updateButton = view.findViewById(R.id.button_update);
updateButton.setOnClickListener(
v -> {
boolean updated = false;
if (!checkTextInputIsNull(labelTextField, /* showError= */ false)) {
setAllTextFieldsLabel(String.valueOf(labelEditText.getText()));
updated = true;
}
if (!checkTextInputIsNull(textInputError, /* showError= */ false)) {
updateErrorText(String.valueOf(inputErrorEditText.getText()), toggleErrorButton);
updated = true;
}
if (!checkTextInputIsNull(helperTextTextField, /* showError= */ false)) {
setAllTextFieldsHelperText(String.valueOf(helperTextEditText.getText()));
updated = true;
}
if (!checkTextInputIsNull(counterMaxTextField, /* showError= */ false)) {
int length = Integer.parseInt(counterEditText.getText().toString());
setAllTextFieldsCounterMax(length);
updated = true;
}
if (!checkTextInputIsNull(placeholderTextField, /* showError= */ false)) {
setAllTextFieldsPlaceholder(String.valueOf(placeholderEditText.getText()));
updated = true;
}
if (updated) {
showToast(R.string.cat_textfield_toast_update_button);
}
});

// Initializing switch to toggle between disabling or enabling text fields.
SwitchMaterial enabledSwitch = view.findViewById(R.id.cat_textfield_enabled_switch);
enabledSwitch.setOnCheckedChangeListener(
Expand Down Expand Up @@ -177,6 +198,20 @@ private void setAllTextFieldsError(String error) {
}
}

private void updateErrorText(String errorText, Button toggleErrorButton) {
this.errorText = errorText;
// if error already showing, call setError again to update its text.
if (toggleErrorButton
.getText()
.toString()
.equals(getResources().getString(R.string.cat_textfield_hide_error_text))) {
for (TextInputLayout textfield : textfields) {
setErrorIconClickListeners(textfield);
textfield.setError(errorText);
}
}
}

private void setErrorIconClickListeners(TextInputLayout textfield) {
textfield.setErrorIconOnClickListener(
v -> showToast(R.string.cat_textfield_toast_error_icon_click));
Expand Down Expand Up @@ -205,11 +240,13 @@ private void setAllTextFieldsCounterMax(int length) {
}
}

private boolean checkTextInputIsNull(TextInputLayout textInputLayout) {
private boolean checkTextInputIsNull(TextInputLayout textInputLayout, boolean showError) {
if (textInputLayout.getEditText().getText() == null
|| textInputLayout.getEditText().length() == 0) {
textInputLayout.setError(
getResources().getString(R.string.cat_textfield_null_input_error_text));
if (showError) {
textInputLayout.setError(
getResources().getString(R.string.cat_textfield_null_input_error_text));
}
return true;
}
textInputLayout.setError(null);
Expand Down
Expand Up @@ -146,6 +146,14 @@
android:inputType="text"
android:imeOptions="actionDone"/>
</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/button_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/cat_textfield_standard_spacing"
android:layout_gravity="center_horizontal"
android:text="@string/cat_textfield_update_button"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
Expand Up @@ -68,7 +68,7 @@

<string name="cat_textfield_customize_section_title" translatable="false">Customize text fields</string>
<string name="cat_textfield_customize_section_description" translatable="false">
Enter values below to customize the text fields above. Press \"done\" to update each field.
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.
</string>
<string name="cat_textfield_label" translatable="false">Label</string>
<string name="cat_textfield_password" translatable="false">Password</string>
Expand All @@ -94,10 +94,12 @@

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

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

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

0 comments on commit a3b5440

Please sign in to comment.