Skip to content

Commit

Permalink
[ExposedDropdownMenu] Added attribute to set dropdown menu's container.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 528578402
  • Loading branch information
pekingme authored and leticiarossi committed May 2, 2023
1 parent 1a2078b commit 1562d0b
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 19 deletions.
26 changes: 13 additions & 13 deletions docs/components/Menu.md
Expand Up @@ -564,19 +564,19 @@ For all attributes that apply to the `TextInputLayout`, see the

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

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

#### Styles

Expand Down
Expand Up @@ -172,13 +172,31 @@ public static MaterialShapeDrawable createWithElevationOverlay(Context context)
* when the overlay will be active.
*/
@NonNull
public static MaterialShapeDrawable createWithElevationOverlay(Context context, float elevation) {
int colorSurface =
MaterialColors.getColor(
context, R.attr.colorSurface, MaterialShapeDrawable.class.getSimpleName());
public static MaterialShapeDrawable createWithElevationOverlay(
@NonNull Context context, float elevation) {
return createWithElevationOverlay(context, elevation, /* backgroundTint= */ null);
}

/**
* Returns a {@code MaterialShapeDrawable} with the elevation overlay functionality initialized, a
* fill color of {@code backgroundTint}, and an elevation of {@code elevation}. When {@code
* backgroundTint} is {@code null}, {@code colorSurface} will be used as default.
*
* <p>See {@link ElevationOverlayProvider#compositeOverlayIfNeeded(int, float)} for information on
* when the overlay will be active.
*/
@NonNull
public static MaterialShapeDrawable createWithElevationOverlay(
@NonNull Context context, float elevation, @Nullable ColorStateList backgroundTint) {
if (backgroundTint == null) {
final int colorSurface =
MaterialColors.getColor(
context, R.attr.colorSurface, MaterialShapeDrawable.class.getSimpleName());
backgroundTint = ColorStateList.valueOf(colorSurface);
}
MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable();
materialShapeDrawable.initializeElevationOverlay(context);
materialShapeDrawable.setFillColor(ColorStateList.valueOf(colorSurface));
materialShapeDrawable.setFillColor(backgroundTint);
materialShapeDrawable.setElevation(elevation);
return materialShapeDrawable;
}
Expand Down
Expand Up @@ -47,6 +47,7 @@
import android.widget.ListAdapter;
import android.widget.TextView;
import androidx.annotation.ArrayRes;
import androidx.annotation.ColorInt;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -56,6 +57,7 @@
import com.google.android.material.internal.ManufacturerUtils;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.shape.MaterialShapeDrawable;

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

Expand Down Expand Up @@ -123,6 +126,14 @@ public MaterialAutoCompleteTextView(
R.styleable.MaterialAutoCompleteTextView_android_popupElevation,
R.dimen.mtrl_exposed_dropdown_menu_popup_elevation);

if (attributes.hasValue(R.styleable.MaterialAutoCompleteTextView_dropDownBackgroundTint)) {
dropDownBackgroundTint =
ColorStateList.valueOf(
attributes.getColor(
R.styleable.MaterialAutoCompleteTextView_dropDownBackgroundTint,
Color.TRANSPARENT));
}

simpleItemSelectedColor =
attributes.getColor(
R.styleable.MaterialAutoCompleteTextView_simpleItemSelectedColor, Color.TRANSPARENT);
Expand Down Expand Up @@ -246,6 +257,54 @@ public void setSimpleItems(@NonNull String[] stringArray) {
setAdapter(new MaterialArrayAdapter<>(getContext(), simpleItemLayout, stringArray));
}

/**
* Sets the color of the popup dropdown container. It will take effect only if the popup
* background is a {@link MaterialShapeDrawable}, which is the default when using a Material
* theme.
*
* @param dropDownBackgroundColor the popup dropdown container color
* @see #setDropDownBackgroundTintList(ColorStateList)
* @see #getDropDownBackgroundTintList()
* @attr ref
* com.google.android.material.R.styleable#MaterialAutoCompleteTextView_dropDownBackgroundTint
*/
public void setDropDownBackgroundTint(@ColorInt int dropDownBackgroundColor) {
setDropDownBackgroundTintList(ColorStateList.valueOf(dropDownBackgroundColor));
}

/**
* Sets the color of the popup dropdown container. It will take effect only if the popup
* background is a {@link MaterialShapeDrawable}, which is the default when using a Material
* theme.
*
* @param dropDownBackgroundTint the popup dropdown container tint as a {@link ColorStateList}
* object.
* @see #setDropDownBackgroundTint(int)
* @see #getDropDownBackgroundTintList()
* @attr ref
* com.google.android.material.R.styleable#MaterialAutoCompleteTextView_dropDownBackgroundTint
*/
public void setDropDownBackgroundTintList(@Nullable ColorStateList dropDownBackgroundTint) {
this.dropDownBackgroundTint = dropDownBackgroundTint;
Drawable dropDownBackground = getDropDownBackground();
if (dropDownBackground instanceof MaterialShapeDrawable) {
((MaterialShapeDrawable) dropDownBackground).setFillColor(this.dropDownBackgroundTint);
}
}

/**
* Returns the color of the popup dropdown container.
*
* @see #setDropDownBackgroundTint(int)
* @see #setDropDownBackgroundTintList(ColorStateList)
* @attr ref
* com.google.android.material.R.styleable#MaterialAutoCompleteTextView_dropDownBackgroundTint
*/
@Nullable
public ColorStateList getDropDownBackgroundTintList() {
return dropDownBackgroundTint;
}

/**
* Sets the color of the default selected popup dropdown item to be used along with
* {@code R.attr.simpleItemLayout}.
Expand Down
Expand Up @@ -949,8 +949,16 @@ private MaterialShapeDrawable getDropDownMaterialShapeDrawable(boolean roundedTo
.setBottomLeftCornerSize(cornerRadius)
.setBottomRightCornerSize(cornerRadius)
.build();

ColorStateList dropDownBackgroundTint = null;
if (editText instanceof MaterialAutoCompleteTextView) {
MaterialAutoCompleteTextView materialAutoCompleteTextView =
((MaterialAutoCompleteTextView) editText);
dropDownBackgroundTint = materialAutoCompleteTextView.getDropDownBackgroundTintList();
}
MaterialShapeDrawable popupDrawable =
MaterialShapeDrawable.createWithElevationOverlay(getContext(), elevation);
MaterialShapeDrawable.createWithElevationOverlay(
getContext(), elevation, dropDownBackgroundTint);
popupDrawable.setShapeAppearanceModel(shapeAppearanceModel);
popupDrawable.setPadding(0, verticalPadding, 0, verticalPadding);
return popupDrawable;
Expand Down
Expand Up @@ -102,6 +102,7 @@
<public name="simpleItems" type="attr"/>
<public name="simpleItemSelectedColor" type="attr"/>
<public name="simpleItemSelectedRippleColor" type="attr"/>
<public name="dropDownBackgroundTint" type="attr"/>

<public name="Widget.Design.TextInputLayout" type="style"/>
<public name="Widget.MaterialComponents.TextInputLayout.FilledBox" type="style"/>
Expand Down
Expand Up @@ -370,6 +370,8 @@
<attr name="simpleItemSelectedColor" format="color"/>
<!-- The ripple color of the default selected item of the dropdown list. -->
<attr name="simpleItemSelectedRippleColor" format="color"/>
<!-- The container color of the dropdown menu. -->
<attr name="dropDownBackgroundTint" format="color"/>
</declare-styleable>

</resources>

0 comments on commit 1562d0b

Please sign in to comment.