diff --git a/catalog/java/io/material/catalog/textfield/TextFieldControllableDemoFragment.java b/catalog/java/io/material/catalog/textfield/TextFieldControllableDemoFragment.java index a2fdf0bc8..23df88a30 100644 --- a/catalog/java/io/material/catalog/textfield/TextFieldControllableDemoFragment.java +++ b/catalog/java/io/material/catalog/textfield/TextFieldControllableDemoFragment.java @@ -19,18 +19,16 @@ package io.material.catalog.textfield; import io.material.catalog.R; import android.graphics.Color; -import android.os.Bundle; -import android.support.annotation.Nullable; +import android.support.annotation.LayoutRes; +import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputLayout; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; import android.widget.Button; -import io.material.catalog.feature.DemoFragment; -/** A fragment that displays the main text field demos for the Catalog app. */ -public class TextFieldControllableDemoFragment extends DemoFragment { +/** A base class for controllable text field demos in the Catalog app. */ +public abstract class TextFieldControllableDemoFragment extends TextFieldDemoFragment { private int colorIndex = 0; private int[] colors = new int[] { @@ -38,91 +36,80 @@ public class TextFieldControllableDemoFragment extends DemoFragment { }; @Override - public View onCreateDemoView( - LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) { - View view = - layoutInflater.inflate( - R.layout.cat_textfield_controllable_fragment, viewGroup, false /* attachToRoot */); + public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view) { + super.initTextFieldDemoControls(layoutInflater, view); - // Initialize text inputs. - TextInputLayout textInputDemoBoxOutline = view.findViewById(R.id.text_input_demo_box_outline); - - TextInputLayout textInputError = view.findViewById(R.id.text_input_error); - TextInputLayout textInputLabel = view.findViewById(R.id.text_input_label); - TextInputLayout textInputCounterMax = view.findViewById(R.id.text_input_counter_max); - - // Initialize button for changing the outline box color. + // Initialize button for updating the box color. Button changeColorButton = view.findViewById(R.id.button_change_color); - changeColorButton.setOnClickListener( - new OnClickListener() { - @Override - public void onClick(View v) { - textInputDemoBoxOutline.setBoxStrokeColor(getNextColor()); - } - }); + changeColorButton.setOnClickListener(v -> changeTextFieldBoxColors(getNextColor())); + + // Initialize button for updating the label text. + TextInputLayout labelTextField = view.findViewById(R.id.text_input_label); + view.findViewById(R.id.button_update_label_text) + .setOnClickListener( + v -> { + if (!checkTextInputIsNull(labelTextField)) { + setAllTextFieldsLabel(String.valueOf(labelTextField.getEditText().getText())); + } + }); // Initialize button for toggling the error text visibility. Button toggleErrorButton = view.findViewById(R.id.button_toggle_error); toggleErrorButton.setOnClickListener( - new OnClickListener() { - @Override - public void onClick(View v) { - if (textInputDemoBoxOutline.getError() == null) { - if (textInputError.getEditText().length() == 0) { - textInputDemoBoxOutline.setError( - getResources().getString(R.string.cat_textfield_error)); - } else { - textInputDemoBoxOutline.setError(textInputError.getEditText().getText()); - } - toggleErrorButton.setText( - getResources().getString(R.string.cat_textfield_hide_error_text)); - } else { - textInputDemoBoxOutline.setError(null); - toggleErrorButton.setText( - getResources().getString(R.string.cat_textfield_show_error_text)); - } + v -> { + if (!textfields.isEmpty() && textfields.get(0).getError() == null) { + TextInputEditText errorEditText = view.findViewById(R.id.edit_text_error); + String error = + !TextUtils.isEmpty(errorEditText.getText()) + ? String.valueOf(errorEditText.getText()) + : getResources().getString(R.string.cat_textfield_error); + setAllTextFieldsError(error); + toggleErrorButton.setText( + getResources().getString(R.string.cat_textfield_hide_error_text)); + } else { + setAllTextFieldsError(null); + toggleErrorButton.setText( + getResources().getString(R.string.cat_textfield_show_error_text)); } }); - // Initialize button for updating the label text. - view.findViewById(R.id.button_update_label_text) - .setOnClickListener( - new OnClickListener() { - @Override - public void onClick(View v) { - if (!checkTextInputIsNull(textInputLabel)) { - textInputDemoBoxOutline.setHint(textInputLabel.getEditText().getText()); - } - } - }); - - // Initialize button for updating the error text. - view.findViewById(R.id.button_update_error_text) - .setOnClickListener( - new OnClickListener() { - @Override - public void onClick(View v) { - if (!checkTextInputIsNull(textInputError)) { - textInputDemoBoxOutline.setError(textInputError.getEditText().getText()); - toggleErrorButton.setText( - getResources().getString(R.string.cat_textfield_hide_error_text)); - } - } - }); - // Initialize button for updating the counter max. + TextInputLayout counterMaxTextField = view.findViewById(R.id.text_input_counter_max); view.findViewById(R.id.button_counter_max) .setOnClickListener( - new OnClickListener() { - @Override - public void onClick(View v) { - if (!checkTextInputIsNull(textInputCounterMax)) { - textInputDemoBoxOutline.setCounterMaxLength( - Integer.parseInt(textInputCounterMax.getEditText().getText().toString())); - } + v -> { + if (!checkTextInputIsNull(counterMaxTextField)) { + int length = + Integer.parseInt(counterMaxTextField.getEditText().getText().toString()); + setAllTextFieldsCounterMax(length); } }); - return view; + } + + private void changeTextFieldBoxColors(int color) { + for (TextInputLayout textfield : textfields) { + onChangeTextFieldBoxColors(textfield, color); + } + } + + public abstract void onChangeTextFieldBoxColors(TextInputLayout textfield, int color); + + private void setAllTextFieldsLabel(String label) { + for (TextInputLayout textfield : textfields) { + textfield.setHint(label); + } + } + + private void setAllTextFieldsError(String error) { + for (TextInputLayout textfield : textfields) { + textfield.setError(error); + } + } + + private void setAllTextFieldsCounterMax(int length) { + for (TextInputLayout textfield : textfields) { + textfield.setCounterMaxLength(length); + } } private boolean checkTextInputIsNull(TextInputLayout textInputLayout) { @@ -140,4 +127,10 @@ public class TextFieldControllableDemoFragment extends DemoFragment { colorIndex = (colorIndex + 1) % colors.length; return colors[colorIndex]; } + + @Override + @LayoutRes + public int getTextFieldDemoControlsLayout() { + return R.layout.cat_textfield_controls; + } } diff --git a/catalog/java/io/material/catalog/textfield/TextFieldDemoFragment.java b/catalog/java/io/material/catalog/textfield/TextFieldDemoFragment.java new file mode 100644 index 000000000..f51f7a6ef --- /dev/null +++ b/catalog/java/io/material/catalog/textfield/TextFieldDemoFragment.java @@ -0,0 +1,86 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.material.catalog.textfield; + +import io.material.catalog.R; + +import android.os.Bundle; +import android.support.annotation.LayoutRes; +import android.support.annotation.Nullable; +import com.google.android.material.textfield.TextInputLayout; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import io.material.catalog.feature.DemoFragment; +import io.material.catalog.feature.DemoUtils; +import java.util.List; + +/** + * Base class that provides a structure for text field demos with optional controls for the Catalog + * app. + */ +public abstract class TextFieldDemoFragment extends DemoFragment { + protected List textfields; + + @Override + public View onCreateDemoView( + LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) { + View view = + layoutInflater.inflate( + R.layout.cat_textfield_fragment, viewGroup, false /* attachToRoot */); + initTextFields(layoutInflater, view); + initTextFieldDemoControls(layoutInflater, view); + return view; + } + + private void initTextFields(LayoutInflater layoutInflater, View view) { + inflateTextFields(layoutInflater, view.findViewById(R.id.content)); + // Add text fields from the content layout before the text fields from the demo controls to + // allow for modifying the demo text fields without modifying the textfields used for the + // demo controls. + addTextFieldsToList(view); + } + + private void inflateTextFields(LayoutInflater layoutInflater, ViewGroup content) { + content.addView(layoutInflater.inflate(getTextFieldContent(), content, false)); + } + + public void initTextFieldDemoControls(LayoutInflater layoutInflater, View view) { + inflateTextFieldDemoControls(layoutInflater, view.findViewById(R.id.content)); + } + + private void inflateTextFieldDemoControls(LayoutInflater layoutInflater, ViewGroup content) { + @LayoutRes int demoControls = getTextFieldDemoControlsLayout(); + if (demoControls != 0) { + content.addView(layoutInflater.inflate(getTextFieldDemoControlsLayout(), content, false)); + } + } + + private void addTextFieldsToList(View view) { + textfields = DemoUtils.findViewsWithType(view, TextInputLayout.class); + } + + @LayoutRes + public int getTextFieldContent() { + return R.layout.cat_textfield_content; + } + + @LayoutRes + public int getTextFieldDemoControlsLayout() { + return 0; + } +} diff --git a/catalog/java/io/material/catalog/textfield/TextFieldFilledDemoFragment.java b/catalog/java/io/material/catalog/textfield/TextFieldFilledDemoFragment.java new file mode 100644 index 000000000..e04f8f0fe --- /dev/null +++ b/catalog/java/io/material/catalog/textfield/TextFieldFilledDemoFragment.java @@ -0,0 +1,37 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.material.catalog.textfield; + +import io.material.catalog.R; + +import android.support.annotation.LayoutRes; +import com.google.android.material.textfield.TextInputLayout; + +/** A fragment that displays the filled text field demos with controls for the Catalog app. */ +public class TextFieldFilledDemoFragment extends TextFieldControllableDemoFragment { + + @Override + public void onChangeTextFieldBoxColors(TextInputLayout textfield, int color) { + textfield.setBoxBackgroundColor(color); + } + + @Override + @LayoutRes + public int getTextFieldContent() { + return R.layout.cat_textfield_filled_content; + } +} diff --git a/catalog/java/io/material/catalog/textfield/TextFieldFragment.java b/catalog/java/io/material/catalog/textfield/TextFieldFragment.java index ed02ffc40..ade1b3ccc 100644 --- a/catalog/java/io/material/catalog/textfield/TextFieldFragment.java +++ b/catalog/java/io/material/catalog/textfield/TextFieldFragment.java @@ -57,10 +57,17 @@ public class TextFieldFragment extends DemoLandingFragment { public List getAdditionalDemos() { List additionalDemos = new ArrayList<>(); additionalDemos.add( - new Demo(R.string.cat_textfield_controllable_demo_title) { + new Demo(R.string.cat_textfield_filled_demo_title) { @Override public Fragment createFragment() { - return new TextFieldControllableDemoFragment(); + return new TextFieldFilledDemoFragment(); + } + }); + additionalDemos.add( + new Demo(R.string.cat_textfield_outline_demo_title) { + @Override + public Fragment createFragment() { + return new TextFieldOutlineDemoFragment(); } }); return additionalDemos; diff --git a/catalog/java/io/material/catalog/textfield/TextFieldMainDemoFragment.java b/catalog/java/io/material/catalog/textfield/TextFieldMainDemoFragment.java index be8c2222a..4c200e650 100644 --- a/catalog/java/io/material/catalog/textfield/TextFieldMainDemoFragment.java +++ b/catalog/java/io/material/catalog/textfield/TextFieldMainDemoFragment.java @@ -24,19 +24,19 @@ import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import io.material.catalog.feature.DemoFragment; /** A fragment that displays the main text field demos for the Catalog app. */ -public class TextFieldMainDemoFragment extends DemoFragment { +public class TextFieldMainDemoFragment extends TextFieldDemoFragment { @Override public View onCreateDemoView( LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) { - return layoutInflater.inflate(getTextfieldsContent(), viewGroup, false /* attachToRoot */); + return layoutInflater.inflate(getTextFieldContent(), viewGroup, false /* attachToRoot */); } + @Override @LayoutRes - protected int getTextfieldsContent() { + public int getTextFieldContent() { return R.layout.cat_textfield_content; } } diff --git a/catalog/java/io/material/catalog/textfield/TextFieldOutlineDemoFragment.java b/catalog/java/io/material/catalog/textfield/TextFieldOutlineDemoFragment.java new file mode 100644 index 000000000..35b7741e0 --- /dev/null +++ b/catalog/java/io/material/catalog/textfield/TextFieldOutlineDemoFragment.java @@ -0,0 +1,37 @@ +/* + * Copyright 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.material.catalog.textfield; + +import io.material.catalog.R; + +import android.support.annotation.LayoutRes; +import com.google.android.material.textfield.TextInputLayout; + +/** A fragment that displays the outline text field demos with controls for the Catalog app. */ +public class TextFieldOutlineDemoFragment extends TextFieldControllableDemoFragment { + + @Override + public void onChangeTextFieldBoxColors(TextInputLayout textfield, int color) { + textfield.setBoxStrokeColor(color); + } + + @Override + @LayoutRes + public int getTextFieldContent() { + return R.layout.cat_textfield_outline_content; + } +} diff --git a/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_controllable_fragment.xml b/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_controllable_fragment.xml deleted file mode 100644 index 62d42135a..000000000 --- a/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_controllable_fragment.xml +++ /dev/null @@ -1,219 +0,0 @@ - - - - - - - - - -