Reputation: 81
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_search"
android:icon="@drawable/search"
appcompat:actionViewClass="android.support.v7.widget.SearchView"
appcompat:showAsAction="ifRoom"
android:title="Search"/>
</menu>
I want to change the search image in the top right corner, currently it is black and I want to change it to orange
Upvotes: 1
Views: 1186
Reputation: 254
You can reset the color of your MenuItem by setColorFilter
.
Drawable can be changed to any color.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_menu_frag, menu);
// replace MenuItem with your own
MenuItem item = menu.getItem(0);
final Drawable drawable = item.getIcon();
if (drawable != null) {
final int color = Color.RED;
drawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
}
}
Upvotes: 1
Reputation: 244
Use below XML in your menu item
<item
android:id="@+id/menu_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/search"
android:showAsAction="ifRoom|collapseActionView"
android:title="@string/menu_search" />
// Please make sure you using same menu item xml in your activity or fragment.
Upvotes: 0
Reputation: 1075
In onCreateOptionsMenu(Menu menu, MenuInflater inflater)
you can use this:
Note: You may want to use PorterDuff.Mode.SRC_IN
instead of PorterDuff.Mode.MULTIPLY
)
MenuItem searchItem = menu.findItem(R.id.action_search);
int color = 0; // SPECIFY THE COLOUR YOU WANT HERE. I suggest you retrieve colour from colors.xml
menuItem.getIcon().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
But personally I would probably just change the colour of the drawable manually (i.e. replace the drawable with a coloured one).
Upvotes: 0
Reputation: 1023
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_notifications, menu);
this.menu = menu;
super.onCreateOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_clear_all) {
showToast("Clear All");
return true;
}
return super.onOptionsItemSelected(item);
}
Create xml file under res>menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".YourActivityName" >
<item
android:id="@+id/action_clear_all"
android:title="@string/noti_clear_all"
android:icon="@drawable/ic_clear_all"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
</menu>
Upvotes: 1