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.
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.
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.
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
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>
searchView.setBackgroundColor(Color.argb( 164, 200, 57, 1));
Or:
searchView.setBackgroundColor(Color.rgb(116,0,0));
insert this in:
public boolean onCreateOptionsMenu(Menu menu)
Related
I'm using an exposed dropdown from material design components. It's an AutoCompleteTextView within a TextInputLayout which basically acts like a spinner. You give it an adapter and it pops up a PopupWindow on click.
The PopupWindow background is a dark gray. I'd like to change it to white. I've tried this via theming to no avail:
<style name="PopupWindow">
<item name="popupMenuBackground">#color/white</item>
<item name="android:popupBackground">#color/white</item>
<item name="android:windowBackground">#color/white</item>
</style>
I've also tried calling this on the AutoCompleteTextView:
autoCompleteTextView.setDropDownBackgroundResource(R.color.white);
This actually worked. However, if I open the dropdown when the keyboard is open, the background turns dark gray again. It's only when the keyboard is dismissed that the background is white.
Any idea how to solve this? Surely there has to be a theme attribute that I can override to make the PopupWindow background white?
Found the solution thanks to this post: https://medium.com/#rmirabelle/there-is-no-material-design-spinner-for-android-3261b7c77da8
Just override colorSurface in your theme:
<item name="colorSurface">#color/white</item>
I know this question must be asked before, but I don't know where can I find the solution since I don't know what is the name.
I want to change the color to red. How can I do to achieve this ?
Thanks a lot !
A style on Android contains a number of different attributes. This can control background color, text color, font styling, etc.
The Theme.AppCompat.Light.DarkActionBar style contains several attributes for an overall light theme with a dark colored action bar.
If the background color you want to use needs white text and icons on top of it, you reference this as the parent and just change the background color.
<item name="android:background">#ff0000</item>
If your background color would look better with black (or dark) text, you could set the parent to Theme.AppCompat.Light.
If you want to change the color of the application bar (and the application's primary color – the application bar inherits it), just change your colorPrimary attribute. Like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/yourColorResource</item>
</style>
Instead of #color/yourColorResource, you can directly use a color's hexcode or Android's pre-defined resources such as #android:color/holo_red_dark
For the status bar's color, you can use similarly use the colorPrimaryDark attribute. You can read more about this here.
I need to change the underline color of the ActionBar tabs programmatically. I've checked around SO and found nothing; I've looked at this question, but none of the answers helped.
I know how to change the color in styles.xml like so:
<style name="ActionBarTabStyle" parent="#android:style/Widget.Holo.ActionBar.TabView">
<item name="android:background">#color/red</item>
</style>
I know how to change the background color, but I need to change the color of the white line below Tab 1.
I need to change this color while the app is running, using Java code. Is there any way to do this?
there are two ways you can do this
you can get action bar instance and call actionbar.setBackgroundDrawable with this you can change background color of the action bar
else define two different custom themes in app and change the theme programatically.
I have a custom theme created with this generator. It has a custom style for Spinners which I don't like. I want to change the background drawable but I can't seem to figure out which property controls this.
This is what the themed version looks like
And here is what it will look like when using the Holo.Light theme.
Notice the dark gray lines around the dropdown list in the first (themed) image. This is what I want to get rid of. What property controls this? I want them to match the default.
Also, what controls the vertical aligment of the dropdown list? As you can see, it is overlapping with the Spinner in the first image (the line under it isn't visible as it is in the second image).
The attribute you want is android:popupBackground on the Spinner element.
If you look closely, the holo popup also overlaps the spinner some, but there is a bunch of padding for the drop shadow, so it looks good.
However, you can use android:dropDownVerticalOffset on the Spinner element to adjust it.
We came across the same issue. It has to do with the Android Holo theme generator.
Here are the lines that you should remove from your Theme.xml file...
<item name="android:spinnerStyle">#style/SpinnerCustom</item>
<item name="android:spinnerDropDownItemStyle">#style/SpinnerDropDownItemCustom</item>
<item name="android:popupMenuStyle">#style/PopupMenu.Custom</item>
<item name="android:dropDownListViewStyle">#style/DropDownListView.Custom</item>
<item name="android:actionDropDownStyle">#style/DropDownNav.Custom</item>
...by removing these, we now have the standard Holo.Light theme on the spinner dropdowns.
You need to change the parent of spinnertheme to android:Widget.Holo.Light.Spinner
<style name="SpinnerTHEME" parent="android:Widget.Holo.Light.Spinner">
<style name="spinner_style" parent="android:Widget.Holo.Light.Spinner">
<item name="android:paddingLeft">#dimen/ten_dp</item>
<item name="android:paddingRight">#dimen/ten_dp</item>
</style>
I've used "Android Action Bar Style Generator" in order to generate my own Holo theme.
Then I've added two icons to the action bar, one for a searchFilter and one for a file picker, both icons are white. File picker icon (at the right) looks good, but somehow search icon looks dark grey. When typing on the search text view, close button also looks dark grey.
I've tried changing color of the actionbar and also from the searchview, without success. Why is the search view showing this color? How can I change it?
Thank you very much.
UPDATE: I thought that my app was using my icon but it doesn't, it uses the default search view icon, so my question actually is, how do I change the default icon of the search view?
This sentence at the menu.xml file is not working:
android:icon="#drawable/selectable_header_search"
Unfortunately, there is no easy way to change the SearchView icon to a custom drawable, since the theme attribute searchViewSearchIcon is not public. See this answer for details.
However, I think that your problem is caused by inheriting from the wrong theme. Please use android:Theme.Holo.Light.DarkActionBar as the base for your theme. Then the default icons on the action bar should have a light color.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
...
</style>
Since the searchViewSearchIcon attribute is not public, you can change the icon using the following code:
int searchIconId = searchView.getContext().getResources().getIdentifier("android:id/search_button",null, null);
ImageView searchIcon = (ImageView) searchView.findViewById(searchIconId);
searchIcon.setImageResource(R.drawable........);
Try to set up in the showAsAction this option: *MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW*
Without this option the SearchView do not take the icon which you setup for action button.
menu.add("Search")
.setIcon(
getResources().getDrawable(
R.drawable.selectable_header_search))
.setActionView(searchView)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
The easiest way I've found to change the default searchview icon in the action bar is in your onPrepareOptionsMenu. You can see my answer here to a similar question!
In your theme style include this:
<style name="YourTheme.Toolbar">
<item name="searchViewStyle">#style/YourActionBarSearch</item>
<item name="editTextColor">#color/your_ab_text_color</item>
<item name="android:textColorHint">#color/your_ab_hint_color</item>
</style>
You can now define the style of the SearchView with searchViewStyle, and editTextColor will set the color of the text in the search box. By setting android:textColorHint you will also set the color of the hint, e.g. "Search...".
<style name="YourActionBarSearch" parent="#style/Widget.Holo.SearchView">
<item name="searchIcon">#drawable/ic_ab_search</item>
<item name="queryBackground">#null</item>
<item name="submitBackground">#null</item>
<item name="searchHintIcon">#drawable/ic_ab_small_search</item>
<item name="defaultQueryHint">#string/toolbar_search_hint</item>
<item name="closeIcon">#drawable/ic_ab_small_search_close</item>
</style>
This will style your search view as you like, more or less. The field searchIcon is the icon in the action bar. The field searchHintIcon is the small icon to the left of the hint in the search field. The field closeIcon is the close icon at the right side of the search field.
Check this out, for more style settings:
$ANDROID_SDK/platforms/android-24/data/res/values/styles_holo.xml
For those like me hoping to find a way to truly change the icon, you can use this with actionbar sherlock:
// Getting the 'search_icon'
ImageView searchIcon = (ImageView)searchView.findViewById(R.id.abs__search_button);
// Setting background of 'search_plate' to earlier defined drawable.
searchIcon.setImageResource(R.drawable.search_button_selector);
You can't change the default one with the common way android:icon, because the field related to it had been set to private.