How to change SearchView default icon? - android

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.

Related

Android ActionMode styling, cannot set menu item colour

I am trying to set the text colour for all the text inside a ActionMode in android. I have found ways to change TitleTextColor and SubTitleTextColor but I cannot find a way to style the actual non-collapsed menu item text color.
I am using the android.support.v7.widget.toolbar using the statSupportActionMode(ActionMode.Callback) to display the action mode.
Thanks in advance,
Thomas.
<style name="ThemeName" parent="#style/Theme.Sherlock.Light">
<item name="actionMenuTextColor">#color/white</item>
<item name="android:actionMenuTextColor">#color/white</item>
</style>
I used the iconTint attribute to style the menu items. Grab the xml file you use to inflate the menu then add the following attribute for each menu items that you want to style.
android:iconTint="#fff"
The above line of code will render the icons white. It's better to use your theme's color in styling.

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.
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)

Android - Change ActionBar tab underline color programatically

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.

How do I change the color of actionbar's up arrow?

How do I change the actionbar's up arrow. I am using the action bar in android not ABS or actionbarcompat. Is there a way to change the color /image of the action bar's up arrow ?
The answer is provided by Jake Wharton in How to customize the back button on ActionBar
<style name="Theme.MyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">#drawable/my_up_indicator</item>
</style>
If you are using Toolbar, you can change the color by just changing the theme. You only have to change the style of the toolbar
app:theme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
If you are using a dark.actionBar your back button is going to be white else if you are using light actionbar theme it is going to be black.
Update:
If you want to modify the color as you wish (other then black or white) you can define a new style, parent one of the above themes and set colorControlNormal or android:textColorSecondary values to your desired color.
the actionbar's up arrow is an image.
you can change it by download a new one and replace it:
download new image,best is to download it from google material design icon page.download it in black or white in png format.save it somewhere you can find it easily.a good choice for download is the "navigate_before" icon .
insert the image to the studio: file->new->image asset. rename the image to your preffered name ( ic_back for example).
connect the up arrow button to the new image:
inside the values/styles.xml file add the item tag like that:
<style name="Theme.MyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">#mipmap/ic_navigate_before</item>
</style>
I put the image asset inside the mipmap folder so I wrote "#mipmap" plus "/" and then the image name (mine was ic_navigate_before).
hope it helped!

Android Tab underline color not changing

So I've basically looked into every resource I saw online on how to change the blue underline in the tabs, but all of the advice has not worked in my case.
I tried actionbarsherlock, appcombat, holo from Action Bar Generator but all the coloring did was color the top action bar, not the tab underline. And yes the files did compile and did not have any errors, but for some reason, it seemed like the underline would never be changed even through the generator.
I am confused on how to change the default blue underline on the tabs, and I would SO EXTREMELY appreciate it if there was a working custom style xml that you would share.
After going to Changing ActionBar tabs underline color programmatically result is still same :/
Screen shot after trying from other page: http://i.stack.imgur.com/EOUbu.png
Anyone able to help me out?
I've been struggling with this for days, but finally found the solution. I'm using AppCompat. You can set colorAccent in your theme and that will change the highlight color on your ActionBar. Like so:
<item name="colorAccent">#color/highlightcolor</item>
Here it is in context:
<style name="LightTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/darkgrey</item>
<item name="colorPrimaryDark">#color/black</item>
<item name="colorAccent">#color/highlightcolor</item>
</style>
You need to define a custom theme for that, and then check the states to change the colour of the line. See if this answer helps you
Not sure if you are still pursuing this, but can you show what you did with Action Bar Generator? What Style entries did you add (in XML) and which drawables did you add?
I just went through the same process and it worked well with Action Bar Generator; just required a few lines added to the styles.xml, a new xml file in drawables folder, and then about 5 new image drawables into each of the resolution size folders (drawable-hdpi, etc).

Categories

Resources