tolgap
tolgap

Reputation: 9778

Change AppCompat SearchView suggestion background

I want to set the background of the dropdown suggestions of the SearchView in my Theme style. I don't want to do this via layout files, because I want the OS to handle the selection backgrounds themselves. Setting the background programmatically removed list selectors, and I want to avoid that.

example

This is what I have right now. I want the background of the rows to be white. What do I have to change in my styles.xml to achieve that?

PS: i removed the text on the suggestions myself, so it looks like a big dark block.

Upvotes: 8

Views: 2255

Answers (4)

John61590
John61590

Reputation: 1106

I know this is an old question, but here is what worked for me.

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar"> <item name="autoCompleteTextViewStyle">@style/MyAutoCompleteTextView</item> <item name="android:dropDownItemStyle">@style/MyDropDownItemStyle</item> </style>

Below changes the suggestion dropdown background color on light theme.

<style name="MyAutoCompleteTextView" parent="Widget.AppCompat.Light.AutoCompleteTextView"><item name="android:popupBackground">@android:color/white</item></style>

Below changes the suggestion dropdown text color to black on light theme.

<style name="MyDropDownItemStyle" parent="Widget.AppCompat.DropDownItem.Spinner"><item name="android:textColor">@color/black</item></style>

Upvotes: 0

Ahmed
Ahmed

Reputation: 17

searchView.setBackgroundColor(Color.argb( 164, 200, 57, 1));

Or:

searchView.setBackgroundColor(Color.rgb(116,0,0));

insert this in:

public boolean onCreateOptionsMenu(Menu menu)

Upvotes: -2

Zain
Zain

Reputation: 40830

You can simply do this by using

int autoCompleteTextViewID = getResources().getIdentifier("search_src_text", "id", getPackageName());
mSearchAutoCompleteTextView = mSearchView.findViewById(autoCompleteTextViewID);
mSearchAutoCompleteTextView.setDropDownBackgroundResource(R.color.white); // Setting background color of the suggestion drop down list

Upvotes: 0

user
user

Reputation: 87064

The SearchView has its popup based on a standard styled AutoCompleteTextView so you could change the standard style for this widget in your custom theme to obtain the background change. So in the custom theme override the autoCompleteTextViewStyle attribute to point to a new style in which you have to override the popupBackground with the desired background.

Upvotes: 12

Related Questions