I've created a menu with a single item.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/syncButton"
android:title="Sync"
android:icon="#drawable/ic_sub_menu"
app:showAsAction="never"/>
</menu>
This is used on some of my activities in the toolbar, when clicked it drops down a menu, currently there is only one option but in the future it may be more.
Everything works well except the icon, it's a vector image of the traditional 3 dots colored white. Depending on what showAsAction" is set as it changes color.
Currently showAsAction is set to never so it displays a menu when clicked, this is what I want, but the icon changes to a dark grey. If i set this option to "always" then the icon changes to white but I lose the drop down menu.
How can I keep the drop down menu while keeping my icon white ?
If you just want to change the 3-Dots-Icon for the DropDownMenu, you can change this in your Styles.xml:
In your Theme Style define
<item name="android:actionOverflowButtonStyle">#style/MyTheme.OverFlow</item>
and then define the Overflow with your Icon you want to show instead the 3DotsIcon (in your case the Icon with 3 white dots)
<style name="MyTheme.OverFlow">
<item name="android:src">#drawable/yourNewIcon</item>
</style>
Try this code
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.menu_confirm, menu);
MenuItem action_done = menu.findItem(R.id.action_done);
action_done.setIcon(R.drawable.ic_filter);
menuIconColor(action_done, Color.WHITE);
super.onCreateOptionsMenu(menu, menuInflater);
}
public void menuIconColor(MenuItem menuItem, int color) {
Drawable drawable = menuItem.getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
Related
I implemented following function to change the color of all items in actionbar. and i use it to change the color of elements smoothly, when expanding or collapsing CollapsingToolbarLayout.
private void setToolbarElementsColor(int color) {
PorterDuffColorFilter colorFilter
= new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);
toolbar.getNavigationIcon().setColorFilter(colorFilter);
//overflowDrawable is IconicDrawable from com.mikepenz.iconics
if (toolbar.getOverflowIcon() != overflowDrawable){
toolbar.setOverflowIcon(overflowDrawable);
}
overflowDrawable.color(color);
toolbar.setTitleTextColor(color);
for (int i = 0; i< toolbar.getMenu().size(); i++){
Drawable icon = toolbar.getMenu().getItem(i).getIcon();
if (icon !=null) {
// HERE IS THE CODE WITH EXPLAINED PROBLEM
icon.setColorFilter(colorFilter);
}
}
}
And here is code for menu items:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_back"
android:title="#string/add_remove_filter"
app:showAsAction="always" />
<item
android:id="#+id/action_filter"
android:title="#string/add_filter"
app:showAsAction="always" />
<item
android:id="#+id/action_save_report"
app:showAsAction="never"
android:title="#string/action_save_report">
</item>
</menu>
The code works fine. but one strange problem: when i open overflow menu once and close it, the menu icons retain the last color they have before opening overflow menu, and do not change in accordance to other toolbar elements (the new colors i set for all elements).
Example: Elements are black for expanded state with light toolbar, and white for collapsed state with dark toolbar. now if i collapse the toolbar and open the overflow menu and close it, after that the icon colors remain white, regardless of toolbar expanded or collapsed state, even thought the piece of [icon.setColorFilter(colorFilter);] is run correctly.
The problem solves when the activity goes to background and resumes again. e.g. pressing home button and returning to the activity using recent app list or opening new activity from that activity and returning.
I changed the code inside for loop to the code below but no luck (I am using IconicDrawables for menu icons)
IconicsDrawable icon = (IconicsDrawable) toolbar.getMenu().getItem(i).getIcon();
if (icon !=null) {
icon.color(color);
}
Replacing the icon drawable from IconicDrawable to PNG drawable inside drawables folder also didn't solve the problem.
Try to change the colors of the icon like this,using drawable mutation:-
if (icon !=null) {
icon.mutate();
icon.setColorFilter(colorFilter);
}
After some search, I solved the problem by adding the code below:
#Override
public boolean onMenuOpened(int featureId, Menu menu) {
invalidateOptionsMenu();
return super.onMenuOpened(featureId, menu);
}
It just redraws icons just after opening the overflow menu. but the reason of above mentioned problem is yet unknown to me.
I am using a custom toolbar which has a menu icon. Now on clicking this menu icon i want to show the options menu. How can this be done.
I tried adding a onclicklistener to this menu icon
#Override
public void onClick(View v) {
if(v.getId() == R.id.toolbarMenuIcon){
openOptionsMenu();
}
}
This didnt work. Then i added these lines
setSupportActionBar(mBinding.customSelectToolbar.selectionModeToolbar);
in my activitys oncreate() . Also did override
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.selection_mode_menu, menu);
return super.onCreateOptionsMenu(menu);
}
With this i can see the overflow menu when i click the icon. But the problem is that, it adds the default menu icon also to the tool bar and thus my tool bar has two menu now. How can i have only my custom toolbar icon open the options menu
If you want to customize the default overflow menu icon..
Use setOverflowIcon method of your toolbar.
like:
toolbar.setOverflowIcon(ContextCompat.getDrawable(this, R.drawable.your_icon));
I got it working using theme
<style name="OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
<item name="android:src">#drawable/ic_menu_overflow</item>
<item name="android:tint">...</item>
<item name="android:width">..dp</item>
<item name="android:height">..dp</item>
</style>
<style name="OnArrival.toolbarTheme" parent="Theme.OnArrival.Light">
<item name="actionOverflowButtonStyle">#style/OverFlow</item>
</style>
I have a menu with groups that contain items. These items change color, including their icon, like visited links in HTML. I never specified this behavior or color (which I can't find in my resources at all).
Its applying a tint to the whole item, including the icon after I click on it. Here is my XML.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="none">
<item
android:id="#+id/action_one"
android:icon="#drawable/ic_one"
android:title="#string/one"/>
<item
android:id="#+id/action_two"
android:icon="#drawable/two"
android:title="#string/two" />
</group>
</menu>
I also don't see any attribute to stop this behavior? Do I have to modify an app theme or something to disable this? I want all of my items to have the same color, even after they are clicked on.
The issue was that the menu item was being selected programmatically, which I didn't realize was happening.
...
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// menuItem.setChecked(true); <-- was changing the color
...
Commenting out this line leaves the color as the default color.
I have been having a lot of trouble trying to set the action bar text color using android-support-v7-appcompat. I was able to change the selector color from the default color to a light grey however now when I select something the text color changes as well. I want the text color to stay black when the user makes a selection. Any ideas? Thanks!
in my themes.xml, this did the trick
<style name="AppTheme.AppCompat.Light" parent="#style/Theme.AppCompat.Light">
<item name="android:itemTextAppearance">#style/MenuTextAppearance</item>
</style>
<style name="MenuTextAppearance">
<item name="android:textColor">#android:color/black</item>
</style>
Additionally, I found you could use a spannable string to customize the text color of the menuItems programmatically. This caused the app to crash when using appcompay-v7 however others have had success with Sherlock, Holo, etc. Example:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu, menu);
int positionOfMenuItem = 0; // or whatever...
MenuItem item = menu.getItem(positionOfMenuItem);
SpannableString s = new SpannableString("My red MenuItem");
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
item.setTitle(s);
}
https://stackoverflow.com/a/19008593/2820963
This would be great if you want different menu items to have different colors.
I believe there is a style for select items so you will need to explicitly define the text color for when an item is selected or your app will revert to the default textcolor. The attribute should be something like android:textColorHighlight
I need to change the background of menu items only. When I tried with actionBarItemBackground, it changes the background of application logo [As u can see in the attachment] also. Any help or link will be appreciated.
You can use a custom layout for your actionbar item like so:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#id/item_text"
android:showAsAction="always"
android:actionLayout="#layout/action_text"/>
</menu>
This is styleable as you wish. Another possibility would be adding only the ID (item_text in the example) and set the background like so:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.item_text);
item.getActionView().setBackgroundDrawable(new ColorDrawable(/* your color */));
return super.onPrepareOptionsMenu(menu);
}