Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchView drawables in ActionBar appear blurred

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:

enter image description here

If I remove android:actionViewClass="android.widget.SearchView", everything looks normal:

enter image description here

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?

like image 462
Andrii Chernenko Avatar asked Dec 20 '12 18:12

Andrii Chernenko


2 Answers

So I beat up the solution, however, it declares right compatibility only for API 14 and above, if not using ActionBarSherlock library.


First solution (API 14+)

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());

Second solution (ABS compatibility, API 8+)

ACTION BAR SHERLOCK FRAGMENT

SearchView searchView = 
  new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());

ACTION BAR SHERLOCK ACTIVITY

SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());

Third solution (API 11+)

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.

like image 150
Marek Sebera Avatar answered Nov 11 '22 00:11

Marek Sebera


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.

like image 38
Tolgay Toklar Avatar answered Nov 11 '22 02:11

Tolgay Toklar