سؤال

I have implemented a SearchView into the action bar of an activity and it works fine and will display as expected on a Galaxy Nexus and most emulators. However on a Galaxy Note 10.1 and emulators with tablet-sized screens it looks like this:

Screenshot of SearchView menu icon size problem

As you can see the search icon is much smaller than the other icons. How can I get the icon size of the search icon to match the other icons? Or how can I uniform the icon sizes on large screens?

At first I thought perhaps the problem could be related to the size of the drawable, but after checking them I can confirm the icons consistently exist in the projects drawable-hdpi, drawable-mdpi and drawable-xhdpi resource folders and are the same size as the other menu icons. I used this tool to generate the drawables and there are no irregularities in sizes of any of the drawables. I've even tried using the same drawable as one of the other menu icons and the resulting size difference is the same.

The icon remains the same size with the SearchView open (which is fine because otherwise it might overlap the underline of the EditText view), but when the SearchView is closed again the icon is the same size and still much smaller than the rest of the icons and this may be a clue as to why this is happening.

Activity code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.example, menu);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
        final MenuItem item = menu.findItem(R.id.search);
        search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
} else {
    menu.removeItem(R.id.search); // API < 11
    }   
    return true;
}

res/menu/example.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/search"
        android:title="@string/search"
        android:icon="@drawable/search"
        android:showAsAction="ifRoom"
        android:actionViewClass="android.widget.SearchView" />

</menu>

res/xml/searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/search"
    android:hint="@string/search" >
</searchable>

AndroidManifest.xml

<intent-filter>
    <action android:name="android.intent.action.SEARCH" />
</intent-filter>

<meta-data
    android:name="android.app.default_searchable"
    android:value="com.example.ExampleActivity" />
<meta-data
    android:name="android.app.searchable"
    android:resource="@xml/searchable" />

UPDATE:

Apologies this problem was in fact related to drawables. Specifically the size ratio of the menu icons. The hdpi-drawable menu icons were all 64x64 (x2) when they should have been 48x48 (x1.5).

So this problem can be solved by re-sizing all of the mdpi-drawable menu icons to 32x32 and hdpi-drawable to 48x48 and xhdpi-drawable to 64x64 (thanks to @rekire for suggesting this).

The problem was caused by using an older version of this tool to generate the menu icons. So if you have the same problem you could try using the latest version to generate new icons.

هل كانت مفيدة؟

المحلول

The only thing I could imagine that you put a too small image in one of the drawable directories. Please check that the image dimensions grow from ldpi, mdpi, hdpi, xhdpi, xxhdpi to xxxhdpi.

The default pixel demensions are for mdpi 32x32, for hdpi 48x48 and for xhdpi 64x64.

Check also my calculator, it will calculate the right image dimensions for you.

نصائح أخرى

use drawable as bitmap size it use the bitmap xml

How to Resize a Bitmap in Android?

SearchView searchView = ((SearchView) searchMenuItem.getActionView());
            int searchImgId = android.support.v7.appcompat.R.id.search_button;
            Drawable searchIcon = scaled drawable .....
searchView.findViewById(searchImgId)).setImageDrawable(searchIcon);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top