@@ -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 );
0 commit comments