Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set background color on autocomplete exposed dropdown menu?

Currently working on setting up an Exposed Dropdown menu item using the material design components and guidelines for menus. However when displaying my autocomplete text view it does not have the same background as my other text fields.

I have tried setting the boxBackgroundColor attribute on the TextInputLayout to a different color and modifying background on the Autocomplete TextView. Also I have tired seeing if I could modify the popup theme for the menu but have had no luck.

 // In the onCreateView of fragment

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.dropdown_menu_popup_item, getResources().getStringArray(R.array.down_payment_method_selection));
        monthsAtEmploymentSelection.setAdapter(adapter);

<com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:hint="@string/months_at_employment_hint"
                app:boxBackgroundColor="@color/white">

                <AutoCompleteTextView
                    android:id="@+id/months_at_employment_selection"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </com.google.android.material.textfield.TextInputLayout>
<!--dropdown_menu_popup_item.xml-->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:textAppearance="?attr/textAppearanceSubtitle1"
    android:background="@color/white"/>

I would like the background to be transparent or match the current background that is set to white. Currently it is an off white that does not match the fields.

Example of color problems when using extended dropdown menu style

like image 223
Steven Elliott Avatar asked Jan 27 '23 04:01

Steven Elliott


2 Answers

By setting the attribute app:boxBackgroundColor="@color/yourcolor" on the TextInputLayout field in your layout file should correctly modify the background color.

You may also need to set the child elements android:background="@null", in my case the AutoCompleteTextView to correctly honor the boxBackgroundColor attribute. Per documentation the only time you should need to set the child elements background color is with an edittext field however I found I needed it with the autocomplete view due to how the apps bridge theme is set.

like image 96
Steven Elliott Avatar answered Jan 30 '23 00:01

Steven Elliott


Thanks to this issue, I learn that this can be done programatically:

val autoCompleteTv = binding.yourAutoCompleteTextView
autoCompleteTv.setDropDownBackgroundDrawable(
        ColorDrawable(ContextCompat.getColor(context, R.color.your_desired_color))
)
like image 45
Emmanuel Guerra Avatar answered Jan 30 '23 00:01

Emmanuel Guerra