Reputation: 510
I have tried this approach. However, the icon gets blurry on the device. Other answers I could find did not help. Any help would be appreciated.
Code I am using:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
/>
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.toolbar_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_search);
final SearchView searchView = (SearchView) item.getActionView();
ImageView searchCloseImage = searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchCloseImage.setImageResource(R.drawable.close24);
super.onCreateOptionsMenu(menu, inflater);
}
My styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#009688</item>
<item name="colorPrimaryDark">#00796B </item>
<item name="colorAccent">#4CAF50</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
Upvotes: 0
Views: 436
Reputation: 4445
Use this site to convert your image to different sizes : romannurik.github.io
And add your images to Android studio res
-> mipmap
folder (one by one)
These are the different images size :
ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi
Then set image from mipmap :
ImageView searchCloseImage = searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchCloseImage.setImageResource(R.mipmap.close24);
Upvotes: 1