Change background color in searchview autocomplete dropdown

I have a search service with a content provider for custom offers that appear in a drop-down list. However, the drop-down background is dark and the text is black, so it is not very noticeable. My application theme is inherited from Theme.AppCompat.Light, so everything else in my application is black text on a light background.

enter image description here

I want to change the background of the dropdown menu so that the text is readable, but I have not found a way to do this. The closest I got was the following solution: SearchView search style in the drop-down list But when the popup appears, things look messed up.

enter image description here

Is there any working solution for this?

+8
android searchview styles
source share
4 answers

By applying styles, you can change the background color of the drop-down menu and the color and size of the text of the elements

<!-- ToolBar --> <style name="ToolBarStyle" parent="Theme.AppCompat"> <item name="android:textColorPrimary">@android:color/white</item> <item name="android:textColorSecondary">@android:color/white</item> <item name="actionMenuTextColor">@android:color/white</item> <item name="android:dropDownItemStyle">@style/myDropDownItemStyle</item> <item name="android:dropDownListViewStyle">@style/myDropDownListViewStyle</item> </style> <style name="myDropDownItemStyle" parent="Widget.AppCompat.DropDownItem.Spinner"> <item name="android:textColor">@color/secondary_text_default_material_light</item> <item name="android:textSize">14dp</item> </style> <style name="myDropDownListViewStyle" parent="Widget.AppCompat.ListView.DropDown"> <item name="android:background">#FFF</item> </style> 
+15
source share

There is one possibility.

Define the layout of a sentence sentence string, for example. R.layout.li_query_suggestion, which looks like this:

 <?xml version="1.0" encoding="utf-8"?> <TextView android:id="@android:id/text1" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:gravity="center_vertical" android:minHeight="56dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:textColor="@android:color/black" tools:text="Blah blah blah"/> 

Note that the background color is white and the text color is black. You can, of course, change these values.

Then, in the cursor adapter, specify the layout that you created as the line layout. For example:.

 CursorAdapter suggestionAdapter = new SimpleCursorAdapter( getActivity(), R.layout.li_query_suggestion, null, new String[]{"name"}, new int[]{android.R.id.text1}, 0); 

This will give you something similar to the following: enter image description here

Just fyi, my current theme looks like this:

 <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> <item name="colorPrimary">@color/light_blue_500</item> <item name="colorPrimaryDark">@color/light_blue_700</item> <item name="colorAccent">@color/pink_a200</item> <item name="android:windowActionBarOverlay">true</item> <item name="searchViewStyle">@style/CustomSearchView</item> </style> 
+10
source share

I think you should use

 SearchView.SearchAutoComplete autoCompleteTextView = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text); if (autoCompleteTextView != null) { autoCompleteTextView.setDropDownBackgroundDrawable(getResources().getDrawable(R.drawable.abc_popup_background_mtrl_mult)); } 

this can set the SearchAutoComplete pop-up background to Light

+7
source share

A simple solution

 int autoCompleteTextViewID = getResources().getIdentifier("search_src_text", "id", getPackageName()); mSearchAutoCompleteTextView = mSearchView.findViewById(autoCompleteTextViewID); mSearchAutoCompleteTextView.setDropDownBackgroundResource(R.color.white); 

enter image description here

0
source share

All Articles