Sherlock Actionbar change menu background color - android

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

Related

Menu item icon changes color

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

about android toolbar, how to hide defualt setting menu in toolbar

about android Toolbar, how to hide default setting menu in toolbar
in the this activity i hava set a single icon menu,like this –
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title=""
android:id="#+id/menu_add"
android:icon="#drawable/actionbar_add_icon"
android:showAsAction="ifRoom">
</item>
</menu>
it is menu/main_home.xml
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_home, menu);
return super.onCreateOptionsMenu(menu);
}
but it show a three dots menu. i want know why
and how to show sub menu icon image ,it is normal in the Actionbar, but hide in the Toolbar
If you want to empty the contents of the menu, go to the res/menu folder and delete the item tags from the menu_main.xml file. You will also need to delete references to the items in onOptionsItemSelected(MenuItem item). The menu dots will disappear if there are no items.
If you want to completely remove the three dots menu, then go to the onCreateOptions(...) method in your code and delete it, or simply remove the getMenuInflater().inflate(...); portion of it. You can safely remove the onOptionsItemSelected(MenuItem item) method as well because it will have no purpose.
The simplest way to make the menu disappear is to have the onCreateOptions(...) method return false.
this question is solved use app:showAsAction ="ifRoom" not android:showAsAction ="ifRoom"
<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">
<item
android:title=""
android:id="#+id/menu_button"
app:actionLayout="#layout/menu_single_button"
app:showAsAction = "always"
/></menu>

Android item in menu group text and icon color changing

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.

How to update the text of menu in android actionbar

What i have::
I have a menu in actionbar it has two sub-items in it
buffet_map_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_cart"
android:icon="#drawable/ic_action_camera"
android:showAsAction="ifRoom"
android:title="cart">
<menu>
<item
android:id="#+id/action_cart_buy"
android:icon="#drawable/ic_action_camera"
android:title="Sort by newest"/>
<item
android:id="#+id/action_cart_reserve"
android:icon="#drawable/button_reserve_selector"
android:title="Sort by rating"/>
</menu>
</item>
</menu>
What i am trying to do::
I want to update the text dynamically for the menu buttons
action_cart_buy and action_cart_reserve
How to find this resource from the Java code and update the text ?
You can use onPrepareOptionsMenu(Menu) for dynamic updates:
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
You can use it like:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem buyItem = menu.findItem(R.id.action_cart_buy);
MenuItem reserveItem = menu.findItem(R.id.action_cart_reserve);
buyItem.setTitle("New buyItem String");
reserveItem.setTitle("New reserveItem String");
return super.onPrepareOptionsMenu(menu);
}
Take a look at findItem from Menu and than MenuItem, there is a setTitle.
Edit--
There is a topic called "Changing menu items at runtime" in this article which may help you as well.
You have access to the Menu object in onCreateOptionsMenu or onPrepareOptionsMenu (better).
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_cart_buy);
item.setTitle("Any title");
return super.onPrepareOptionsMenu(menu);
}
To make the UI update your ActionBar at the right time, you have to call:
invalidateOptionsMenu();

ToggleButtons as actionBar custom view behaving weirdly.

Firstly, my code for setting up the actionBar. (Using default system actionBar, android 4.2+)
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.listing_group, menu);
MenuItem listItem = menu.findItem(R.id.action_list);
ToggleButton customActionIcon = (ToggleButton) getLayoutInflater().inflate(R.layout.custom_action_bar_icon_view, null);
listItem.setActionView(customActionIcon);
customActionIcon.setTextOff("LIST");
customActionIcon.setTextOn("LIST");
customActionIcon.setTypeface(OswaldRegular());
customActionIcon= (ToggleButton)getLayoutInflater().inflate(R.layout.custom_action_bar_icon_view, null);
customActionIcon.setTextOff("MAP");
customActionIcon.setTextOn("MAP");
customActionIcon.setTypeface(OswaldRegular());
MenuItem mapItem = menu.findItem(R.id.action_map);
mapItem.setActionView(customActionIcon);
return super.onCreateOptionsMenu(menu);
}
Layout xml for Menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_list"
android:showAsAction="always"
/>
<item
android:id="#+id/action_map"
android:showAsAction="always"/>
</menu>
Layout xml for custom actionBar button:
<?xml version="1.0" encoding="utf-8"?>
<ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_bar_toggle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:background="#color/transparent"
android:textColor="#drawable/listing_group_activity_actionbar_text_color_selector">
</ToggleButton>
The Problem:
When the screen with this action bar first loads, both the buttons show up in the deselected color, but they both show the default text of "Off". When I click one of them, they correctly change color AND they change the text to the one I set in onCreateOptionsMenu(). i.e. , one becomes LIST and one becomes MAP . And then they continue to stay that way and function as normal ToggleButtons would. I would like them to start with the correct text showing, and after that is fixed I want one of them to be selected by default.
Any help on the matter is much appreciated! Thanks!
Fixed. it. Had to do customActionIcon.setChecked(true) for the enabled one and setChecked(false) for the disabled one and they start with the right text and right state

Categories

Resources