Skip to content

Commit 1562d0b

Browse files
pekingmeleticiarossi
authored andcommitted
[ExposedDropdownMenu] Added attribute to set dropdown menu's container.
PiperOrigin-RevId: 528578402
1 parent 1a2078b commit 1562d0b

File tree

6 files changed

+107
-19
lines changed

6 files changed

+107
-19
lines changed

docs/components/Menu.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -564,19 +564,19 @@ For all attributes that apply to the `TextInputLayout`, see the
564564

565565
#### `MaterialAutoCompleteTextView` attributes (input text, dropdown menu)
566566

567-
Element | Attribute | Related method(s) | Default value
568-
----------------------------------------- | ------------------------------------------------------------------- | ----------------------------------------------------------------------------- | -------------
569-
**Input text** | `android:text` | `setText`<br/>`getText` | `@null`
570-
**Typography** | `android:textAppearance` | `setTextAppearance` | `?attr/textAppearanceBodyLarge`
571-
**Input accepted** | `android:inputType` | `N/A` | framework's default
572-
**Input text color** | `android:textColor` | `setTextColor`<br/>`getTextColors`<br/>`getCurrentTextColor` | `?android:textColorPrimary`
573-
**Cursor color** | N/A (color comes from the theme attr `?attr/colorControlActivated`) | N/A | `?attr/colorPrimary`
574-
**Dropdown menu<br/>container color** | N/A | N/A | `?attr/colorSurface`
575-
**Dropdown menu elevation** | `android:popupElevation` | `getPopupElevation` | `3dp`
576-
**Simple items** | `app:simpleItems` | `setSimpleItems` | `null`
577-
**Simple item layout** | `app:simpleItemLayout` | N/A | `@layout/m3_auto_complete_simple_item`
578-
**Selected simple item color** | `app:simpleItemSelectedColor` | `setSimpleItemSelectedColor`<br/>`getSimpleItemSelectedColor` | `?attr/colorSurfaceVariant`
579-
**Selected simple item<br/>ripple color** | `app:simpleItemSelectedRippleColor` | `setSimpleItemSelectedRippleColor`<br/>`getSimpleItemSelectedRippleColor` | `@color/m3_simple_item_ripple_color`
567+
Element | Attribute | Related method(s) | Default value
568+
----------------------------------------- | ------------------------------------------------------------------- |-----------------------------------------------------------------------------------------------------| -------------
569+
**Input text** | `android:text` | `setText`<br/>`getText` | `@null`
570+
**Typography** | `android:textAppearance` | `setTextAppearance` | `?attr/textAppearanceBodyLarge`
571+
**Input accepted** | `android:inputType` | `N/A` | framework's default
572+
**Input text color** | `android:textColor` | `setTextColor`<br/>`getTextColors`<br/>`getCurrentTextColor` | `?android:textColorPrimary`
573+
**Cursor color** | N/A (color comes from the theme attr `?attr/colorControlActivated`) | N/A | `?attr/colorPrimary`
574+
**Dropdown menu<br/>container color** | `app:dropDownBackgroundTint` | `setDropDownBackgroundTint`<br/>`setDropDownBackgroundTintList`<br/>`getDropDownBackgroundTintList` | `@null`</br>(which means `colorSurface` with</br> elevation overlay will be used)
575+
**Dropdown menu elevation** | `android:popupElevation` | `getPopupElevation` | `3dp`
576+
**Simple items** | `app:simpleItems` | `setSimpleItems` | `null`
577+
**Simple item layout** | `app:simpleItemLayout` | N/A | `@layout/m3_auto_complete_simple_item`
578+
**Selected simple item color** | `app:simpleItemSelectedColor` | `setSimpleItemSelectedColor`<br/>`getSimpleItemSelectedColor` | `?attr/colorSurfaceVariant`
579+
**Selected simple item<br/>ripple color** | `app:simpleItemSelectedRippleColor` | `setSimpleItemSelectedRippleColor`<br/>`getSimpleItemSelectedRippleColor` | `@color/m3_simple_item_ripple_color`
580580

581581
#### Styles
582582

lib/java/com/google/android/material/shape/MaterialShapeDrawable.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,31 @@ public static MaterialShapeDrawable createWithElevationOverlay(Context context)
172172
* when the overlay will be active.
173173
*/
174174
@NonNull
175-
public static MaterialShapeDrawable createWithElevationOverlay(Context context, float elevation) {
176-
int colorSurface =
177-
MaterialColors.getColor(
178-
context, R.attr.colorSurface, MaterialShapeDrawable.class.getSimpleName());
175+
public static MaterialShapeDrawable createWithElevationOverlay(
176+
@NonNull Context context, float elevation) {
177+
return createWithElevationOverlay(context, elevation, /* backgroundTint= */ null);
178+
}
179+
180+
/**
181+
* Returns a {@code MaterialShapeDrawable} with the elevation overlay functionality initialized, a
182+
* fill color of {@code backgroundTint}, and an elevation of {@code elevation}. When {@code
183+
* backgroundTint} is {@code null}, {@code colorSurface} will be used as default.
184+
*
185+
* <p>See {@link ElevationOverlayProvider#compositeOverlayIfNeeded(int, float)} for information on
186+
* when the overlay will be active.
187+
*/
188+
@NonNull
189+
public static MaterialShapeDrawable createWithElevationOverlay(
190+
@NonNull Context context, float elevation, @Nullable ColorStateList backgroundTint) {
191+
if (backgroundTint == null) {
192+
final int colorSurface =
193+
MaterialColors.getColor(
194+
context, R.attr.colorSurface, MaterialShapeDrawable.class.getSimpleName());
195+
backgroundTint = ColorStateList.valueOf(colorSurface);
196+
}
179197
MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable();
180198
materialShapeDrawable.initializeElevationOverlay(context);
181-
materialShapeDrawable.setFillColor(ColorStateList.valueOf(colorSurface));
199+
materialShapeDrawable.setFillColor(backgroundTint);
182200
materialShapeDrawable.setElevation(elevation);
183201
return materialShapeDrawable;
184202
}

lib/java/com/google/android/material/textfield/MaterialAutoCompleteTextView.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import android.widget.ListAdapter;
4848
import android.widget.TextView;
4949
import androidx.annotation.ArrayRes;
50+
import androidx.annotation.ColorInt;
5051
import androidx.annotation.LayoutRes;
5152
import androidx.annotation.NonNull;
5253
import androidx.annotation.Nullable;
@@ -56,6 +57,7 @@
5657
import com.google.android.material.internal.ManufacturerUtils;
5758
import com.google.android.material.internal.ThemeEnforcement;
5859
import com.google.android.material.resources.MaterialResources;
60+
import com.google.android.material.shape.MaterialShapeDrawable;
5961

6062
/**
6163
* A special sub-class of {@link android.widget.AutoCompleteTextView} that is auto-inflated so that
@@ -77,6 +79,7 @@ public class MaterialAutoCompleteTextView extends AppCompatAutoCompleteTextView
7779
@NonNull private final Rect tempRect = new Rect();
7880
@LayoutRes private final int simpleItemLayout;
7981
private final float popupElevation;
82+
@Nullable private ColorStateList dropDownBackgroundTint;
8083
private int simpleItemSelectedColor;
8184
@Nullable private ColorStateList simpleItemSelectedRippleColor;
8285

@@ -123,6 +126,14 @@ public MaterialAutoCompleteTextView(
123126
R.styleable.MaterialAutoCompleteTextView_android_popupElevation,
124127
R.dimen.mtrl_exposed_dropdown_menu_popup_elevation);
125128

129+
if (attributes.hasValue(R.styleable.MaterialAutoCompleteTextView_dropDownBackgroundTint)) {
130+
dropDownBackgroundTint =
131+
ColorStateList.valueOf(
132+
attributes.getColor(
133+
R.styleable.MaterialAutoCompleteTextView_dropDownBackgroundTint,
134+
Color.TRANSPARENT));
135+
}
136+
126137
simpleItemSelectedColor =
127138
attributes.getColor(
128139
R.styleable.MaterialAutoCompleteTextView_simpleItemSelectedColor, Color.TRANSPARENT);
@@ -246,6 +257,54 @@ public void setSimpleItems(@NonNull String[] stringArray) {
246257
setAdapter(new MaterialArrayAdapter<>(getContext(), simpleItemLayout, stringArray));
247258
}
248259

260+
/**
261+
* Sets the color of the popup dropdown container. It will take effect only if the popup
262+
* background is a {@link MaterialShapeDrawable}, which is the default when using a Material
263+
* theme.
264+
*
265+
* @param dropDownBackgroundColor the popup dropdown container color
266+
* @see #setDropDownBackgroundTintList(ColorStateList)
267+
* @see #getDropDownBackgroundTintList()
268+
* @attr ref
269+
* com.google.android.material.R.styleable#MaterialAutoCompleteTextView_dropDownBackgroundTint
270+
*/
271+
public void setDropDownBackgroundTint(@ColorInt int dropDownBackgroundColor) {
272+
setDropDownBackgroundTintList(ColorStateList.valueOf(dropDownBackgroundColor));
273+
}
274+
275+
/**
276+
* Sets the color of the popup dropdown container. It will take effect only if the popup
277+
* background is a {@link MaterialShapeDrawable}, which is the default when using a Material
278+
* theme.
279+
*
280+
* @param dropDownBackgroundTint the popup dropdown container tint as a {@link ColorStateList}
281+
* object.
282+
* @see #setDropDownBackgroundTint(int)
283+
* @see #getDropDownBackgroundTintList()
284+
* @attr ref
285+
* com.google.android.material.R.styleable#MaterialAutoCompleteTextView_dropDownBackgroundTint
286+
*/
287+
public void setDropDownBackgroundTintList(@Nullable ColorStateList dropDownBackgroundTint) {
288+
this.dropDownBackgroundTint = dropDownBackgroundTint;
289+
Drawable dropDownBackground = getDropDownBackground();
290+
if (dropDownBackground instanceof MaterialShapeDrawable) {
291+
((MaterialShapeDrawable) dropDownBackground).setFillColor(this.dropDownBackgroundTint);
292+
}
293+
}
294+
295+
/**
296+
* Returns the color of the popup dropdown container.
297+
*
298+
* @see #setDropDownBackgroundTint(int)
299+
* @see #setDropDownBackgroundTintList(ColorStateList)
300+
* @attr ref
301+
* com.google.android.material.R.styleable#MaterialAutoCompleteTextView_dropDownBackgroundTint
302+
*/
303+
@Nullable
304+
public ColorStateList getDropDownBackgroundTintList() {
305+
return dropDownBackgroundTint;
306+
}
307+
249308
/**
250309
* Sets the color of the default selected popup dropdown item to be used along with
251310
* {@code R.attr.simpleItemLayout}.

lib/java/com/google/android/material/textfield/TextInputLayout.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,16 @@ private MaterialShapeDrawable getDropDownMaterialShapeDrawable(boolean roundedTo
949949
.setBottomLeftCornerSize(cornerRadius)
950950
.setBottomRightCornerSize(cornerRadius)
951951
.build();
952+
953+
ColorStateList dropDownBackgroundTint = null;
954+
if (editText instanceof MaterialAutoCompleteTextView) {
955+
MaterialAutoCompleteTextView materialAutoCompleteTextView =
956+
((MaterialAutoCompleteTextView) editText);
957+
dropDownBackgroundTint = materialAutoCompleteTextView.getDropDownBackgroundTintList();
958+
}
952959
MaterialShapeDrawable popupDrawable =
953-
MaterialShapeDrawable.createWithElevationOverlay(getContext(), elevation);
960+
MaterialShapeDrawable.createWithElevationOverlay(
961+
getContext(), elevation, dropDownBackgroundTint);
954962
popupDrawable.setShapeAppearanceModel(shapeAppearanceModel);
955963
popupDrawable.setPadding(0, verticalPadding, 0, verticalPadding);
956964
return popupDrawable;

lib/java/com/google/android/material/textfield/res-public/values/public.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<public name="simpleItems" type="attr"/>
103103
<public name="simpleItemSelectedColor" type="attr"/>
104104
<public name="simpleItemSelectedRippleColor" type="attr"/>
105+
<public name="dropDownBackgroundTint" type="attr"/>
105106

106107
<public name="Widget.Design.TextInputLayout" type="style"/>
107108
<public name="Widget.MaterialComponents.TextInputLayout.FilledBox" type="style"/>

lib/java/com/google/android/material/textfield/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@
370370
<attr name="simpleItemSelectedColor" format="color"/>
371371
<!-- The ripple color of the default selected item of the dropdown list. -->
372372
<attr name="simpleItemSelectedRippleColor" format="color"/>
373+
<!-- The container color of the dropdown menu. -->
374+
<attr name="dropDownBackgroundTint" format="color"/>
373375
</declare-styleable>
374376

375377
</resources>

0 commit comments

Comments
 (0)