I have the following menu layout for my ActionBar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/itemSearch"
android:icon="@drawable/actionbar_icon_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:title="Search"/>
</menu>
And here's the setup code:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.actionbar_default, menu);
SearchView searchView=(SearchView) menu.findItem(R.id.itemSearch).getActionView();
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
View searchEditText = searchView.findViewById(searchPlateId);
((TextView) searchEditText).setTextColor(Color.WHITE);
searchView.setOnCloseListener(new OnCloseListener() {...});
searchView.setOnQueryTextListener(new OnQueryTextListener() {...});
}
Everything is ok, except for one thing: on my Asus tablet (TF-201, Android 3.2.1) graphics are blurred:
If I remove android:actionViewClass="android.widget.SearchView"
, everything looks normal:
This problem is not reproduced on 4.1.2 emulator. I tried leaving only menu inflation code in my onCreateOptionsMenu()
but that didn't help.
How do I fix this?
So I beat up the solution, however, it declares right compatibility only for API 14 and above, if not using ActionBarSherlock library.
Important is to be connected to Themed Context of application, otherwise you get lowest possible design for SearchView (blurry icons)
Creating SearchView programmatically
FRAGMENT
SearchView searchView =
new SearchView(getActivity().getActionBar().getThemedContext());
ACTIVITY
SearchView searchView = new SearchView(getActionBar().getThemedContext());
ACTION BAR SHERLOCK FRAGMENT
SearchView searchView =
new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
ACTION BAR SHERLOCK ACTIVITY
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
In some cases, setting SearchView.setBackgroundColor(int color)
makes the icon appear less blurry, try Color.WHITE or Color.BLACK
Note that this changes background color for MenuItem whether collapsed or not, and eg. by using Theme Holo.Light.DarkActionBar you need to use right color, according to your ActionBar style.
I experienced same problem but other answer did not resolve my problem. I am extending my activity class from AppCompatActivity
. My searchview icon was looking blurry on Android 4.* and it was looking good on Android 5.* without any problem.
I changed my xml to this:
<item android:id="@+id/action_search"
android:icon="@drawable/ic_search_white_24dp"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:iconifiedByDefault="true" />
And imported
import android.support.v7.widget.SearchView;
Instead of import android.widget.SearchView;
And problem fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With