From 85ed9938d73786ea001ccb4ac2642a1384f673ec Mon Sep 17 00:00:00 2001 From: conradchen Date: Tue, 15 Feb 2022 19:26:45 +0000 Subject: [PATCH] [TextField] Provide set simple items API with default item layout for MaterialAutoCompleteTextView Resolves https://github.com/material-components/material-components-android/issues/692 PiperOrigin-RevId: 428832331 --- ...tFieldExposedDropdownMenuDemoFragment.java | 29 --------------- ...extfield_exposed_dropdown_menu_content.xml | 12 ++++-- docs/components/Menu.md | 21 ++++++++--- docs/components/TextField.md | 14 ++++++- .../MaterialAutoCompleteTextView.java | 37 +++++++++++++++++++ .../textfield/res-public/values/public.xml | 2 + .../layout/m3_auto_complete_simple_item.xml | 20 +++++----- .../layout/mtrl_auto_complete_simple_item.xml | 26 +++++++++++++ .../material/textfield/res/values/attrs.xml | 5 +++ .../material/textfield/res/values/styles.xml | 4 ++ 10 files changed, 120 insertions(+), 50 deletions(-) rename catalog/java/io/material/catalog/textfield/res/layout/cat_exposed_dropdown_popup_item.xml => lib/java/com/google/android/material/textfield/res/layout/m3_auto_complete_simple_item.xml (64%) create mode 100644 lib/java/com/google/android/material/textfield/res/layout/mtrl_auto_complete_simple_item.xml diff --git a/catalog/java/io/material/catalog/textfield/TextFieldExposedDropdownMenuDemoFragment.java b/catalog/java/io/material/catalog/textfield/TextFieldExposedDropdownMenuDemoFragment.java index 70d0398985b..00a30e8897e 100644 --- a/catalog/java/io/material/catalog/textfield/TextFieldExposedDropdownMenuDemoFragment.java +++ b/catalog/java/io/material/catalog/textfield/TextFieldExposedDropdownMenuDemoFragment.java @@ -22,8 +22,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.AutoCompleteTextView; import android.widget.Button; import androidx.annotation.LayoutRes; import androidx.annotation.Nullable; @@ -41,38 +39,11 @@ public int getTextFieldContent() { return R.layout.cat_textfield_exposed_dropdown_menu_content; } - @LayoutRes - public int getAdapterItemLayout() { - return R.layout.cat_exposed_dropdown_popup_item; - } - @Override public View onCreateDemoView( LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) { View view = super.onCreateDemoView(layoutInflater, viewGroup, bundle); - ArrayAdapter adapter = - new ArrayAdapter<>( - getContext(), - getAdapterItemLayout(), - getResources().getStringArray(R.array.cat_textfield_exposed_dropdown_content)); - - AutoCompleteTextView editTextFilledExposedDropdown = - view.findViewById(R.id.filled_exposed_dropdown); - editTextFilledExposedDropdown.setAdapter(adapter); - - AutoCompleteTextView editTextFilledEditableExposedDropdown = - view.findViewById(R.id.filled_exposed_dropdown_editable); - editTextFilledEditableExposedDropdown.setAdapter(adapter); - - AutoCompleteTextView editTextOutlinedExposedDropdown = - view.findViewById(R.id.outlined_exposed_dropdown); - editTextOutlinedExposedDropdown.setAdapter(adapter); - - AutoCompleteTextView editTextOutlinedEditableExposedDropdown = - view.findViewById(R.id.outlined_exposed_dropdown_editable); - editTextOutlinedEditableExposedDropdown.setAdapter(adapter); - // Initialize button for toggling the leading icon's visibility. Button toggleLeadingIconButton = view.findViewById(R.id.button_toggle_leading_icon); toggleLeadingIconButton.setVisibility(View.VISIBLE); diff --git a/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_exposed_dropdown_menu_content.xml b/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_exposed_dropdown_menu_content.xml index aac001cc1e0..2826a0af108 100644 --- a/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_exposed_dropdown_menu_content.xml +++ b/catalog/java/io/material/catalog/textfield/res/layout/cat_textfield_exposed_dropdown_menu_content.xml @@ -37,7 +37,8 @@ android:id="@+id/filled_exposed_dropdown" android:layout_width="match_parent" android:layout_height="wrap_content" - android:inputType="none"/> + android:inputType="none" + app:simpleItems="@array/cat_textfield_exposed_dropdown_content"/> @@ -54,7 +55,8 @@ + android:layout_height="wrap_content" + app:simpleItems="@array/cat_textfield_exposed_dropdown_content"/> + android:inputType="none" + app:simpleItems="@array/cat_textfield_exposed_dropdown_content"/> @@ -88,7 +91,8 @@ + android:layout_height="wrap_content" + app:simpleItems="@array/cat_textfield_exposed_dropdown_content"/> diff --git a/docs/components/Menu.md b/docs/components/Menu.md index ec005bfc806..4c98355bdce 100644 --- a/docs/components/Menu.md +++ b/docs/components/Menu.md @@ -474,9 +474,8 @@ using `Theme.Material3.*` themes. The following is an example of a filled exposed dropdown menu: -![2 menu states with text field element: 1) has "item 1", 2) has an empty text -field and a 4-item menu -container.](assets/menu/menus_exposed_dropdown_filled.png) +![2 menu states with text field element: 1) has "item 1", 2) has "item 1" and a +4-item menu container.](assets/menu/menus_exposed_dropdown_filled.png) In the layout: @@ -492,12 +491,22 @@ In the layout: android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="none" + app:simpleItems="@array/simple_items" /> ``` -In code: +The string array specified by `app:simpleItems` will be used as the default +item strings for auto-completion. Or you can also set it programmatically: + +```kt +val items = arrayOf("Item 1", "Item 2", "Item 3", "Item 4") +(textField.editText as? MaterialAutoCompleteTextView)?.setSimpleItems(items) +``` + +Alternatively, to have more control over the auto-completion items rendering, +you can also provide a custom item adapter by: ```kt val items = listOf("Item 1", "Item 2", "Item 3", "Item 4") @@ -505,7 +514,7 @@ val adapter = ArrayAdapter(requireContext(), R.layout.list_item, items) (textField.editText as? AutoCompleteTextView)?.setAdapter(adapter) ``` -In the item layout (`list_item.xml`): +And a custom item layout (`list_item.xml`): ```xml container color** | N/A | N/A | `?attr/colorSurface` **Dropdown menu elevation** | `android:popupElevation` | N/A | `8dp` +**Simple items** | `app:simpleItems` | `setSimpleItems` | `null` +**Simple item layout** | `app:simpleItemLayout` | N/A | `@layout/m3_auto_complete_simple_item` #### Styles diff --git a/docs/components/TextField.md b/docs/components/TextField.md index 8ac9b75646d..43b047097ae 100644 --- a/docs/components/TextField.md +++ b/docs/components/TextField.md @@ -219,12 +219,22 @@ In the layout: android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="none" + app:simpleItems="@array/simple_items" /> ``` -In code: +The string array specified by `app:simpleItems` will be used as the default +item strings for auto-completion. Or you can also set it programmatically: + +```kt +val items = arrayOf("Item 1", "Item 2", "Item 3", "Item 4") +(textField.editText as? MaterialAutoCompleteTextView)?.setSimpleItems(items) +``` + +Alternatively, to have more control over the auto-completion items rendering, +you can also provide a custom item adapter by: ```kt val items = listOf("Item 1", "Item 2", "Item 3", "Item 4") @@ -232,7 +242,7 @@ val adapter = ArrayAdapter(requireContext(), R.layout.list_item, items) (textField.editText as? AutoCompleteTextView)?.setAdapter(adapter) ``` -In the item layout (`list_item.xml`): +And a custom item layout (`list_item.xml`): ```xml parent, View selectedView, int position, } }); + if (attributes.hasValue(R.styleable.MaterialAutoCompleteTextView_simpleItems)) { + setSimpleItems( + attributes.getResourceId(R.styleable.MaterialAutoCompleteTextView_simpleItems, 0)); + } + attributes.recycle(); } @@ -145,6 +158,30 @@ public void setAdapter(@Nullable T adapter) modalListPopup.setAdapter(getAdapter()); } + /** + * Sets the simple string items of auto-completion with the given string array resource. This + * method will create a default {@link ArrayAdapter} with a default item layout specified by + * {@code R.attr.simpleItemLayout} to display auto-complete items. + * + * @see #setSimpleItems(String[]) + * @see #setAdapter(ListAdapter) + */ + public void setSimpleItems(@ArrayRes int stringArrayResId) { + setSimpleItems(getResources().getStringArray(stringArrayResId)); + } + + /** + * Sets the simple string items of auto-completion with the given string array. This method will + * create a default {@link ArrayAdapter} with a default item layout specified by + * {@code R.attr.simpleItemLayout} to display auto-complete items. + * + * @see #setSimpleItems(int) + * @see #setAdapter(ListAdapter) + */ + public void setSimpleItems(@NonNull String[] stringArray) { + setAdapter(new ArrayAdapter<>(getContext(), simpleItemLayout, stringArray)); + } + @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); diff --git a/lib/java/com/google/android/material/textfield/res-public/values/public.xml b/lib/java/com/google/android/material/textfield/res-public/values/public.xml index 7c3bfb48402..e940423331f 100644 --- a/lib/java/com/google/android/material/textfield/res-public/values/public.xml +++ b/lib/java/com/google/android/material/textfield/res-public/values/public.xml @@ -98,6 +98,8 @@ + + diff --git a/catalog/java/io/material/catalog/textfield/res/layout/cat_exposed_dropdown_popup_item.xml b/lib/java/com/google/android/material/textfield/res/layout/m3_auto_complete_simple_item.xml similarity index 64% rename from catalog/java/io/material/catalog/textfield/res/layout/cat_exposed_dropdown_popup_item.xml rename to lib/java/com/google/android/material/textfield/res/layout/m3_auto_complete_simple_item.xml index 1ff2389d8ef..37f96a2fb0f 100644 --- a/catalog/java/io/material/catalog/textfield/res/layout/cat_exposed_dropdown_popup_item.xml +++ b/lib/java/com/google/android/material/textfield/res/layout/m3_auto_complete_simple_item.xml @@ -1,6 +1,6 @@ + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:paddingBottom="13dp" + android:paddingTop="13dp" + android:paddingLeft="16dp" + android:paddingRight="16dp" + android:ellipsize="end" + android:maxLines="1" + android:textAppearance="?attr/textAppearanceBodyLarge"/> diff --git a/lib/java/com/google/android/material/textfield/res/layout/mtrl_auto_complete_simple_item.xml b/lib/java/com/google/android/material/textfield/res/layout/mtrl_auto_complete_simple_item.xml new file mode 100644 index 00000000000..27475dacaf9 --- /dev/null +++ b/lib/java/com/google/android/material/textfield/res/layout/mtrl_auto_complete_simple_item.xml @@ -0,0 +1,26 @@ + + + diff --git a/lib/java/com/google/android/material/textfield/res/values/attrs.xml b/lib/java/com/google/android/material/textfield/res/values/attrs.xml index ae5020e736a..16b8fc0ad69 100644 --- a/lib/java/com/google/android/material/textfield/res/values/attrs.xml +++ b/lib/java/com/google/android/material/textfield/res/values/attrs.xml @@ -301,6 +301,11 @@ + + + + + diff --git a/lib/java/com/google/android/material/textfield/res/values/styles.xml b/lib/java/com/google/android/material/textfield/res/values/styles.xml index 4bd30fd7a40..97a13e7b136 100644 --- a/lib/java/com/google/android/material/textfield/res/values/styles.xml +++ b/lib/java/com/google/android/material/textfield/res/values/styles.xml @@ -443,18 +443,22 @@