Customize a MenuItem´s background in the ActionBar - android

I need to change the color of a menuItem in an ActionBar (that list that appear when the user press a menuItem in my Action Bar).
Which element of the android:style/Widget.Holo.Light.ActionBar I have to change?
Which style I'll have to create to change the android:style/Widget.Holo.Light.ActionBar?

I am not sure you can do that for a particular menuItem, but you can have a look on a more complete blog post about Styling the ActionBar : Part 1 and Part 2
Edit : after looking at the articles another time, I think what you want to do is not possible. The best thing is to create an icon with the appropriate color of your menuitem and to change it when you want.
menu.xml :
<item android:id="#+id/my_menu_item"
android:icon="#drawable/my_menu_item_icon_color1"
android:title="#string/my_menu_item"
android:showAsAction="ifRoom" />
in your activity :
/**
* Create default menu and keep it in a private var
*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
mMenuActionBar = menu;
return true;
}
/**
* Method changing your item to second icon
*/
private void updateActionBar() {
if (mMenuActionBar != null) {
MenuItem menuItem = mMenuActionBar.findItem(R.id.my_menu_item);
if (menuItem != null) {
menuItem.setIcon(#drawable/my_menu_item_icon_color2);
invalidateOptionsMenu();
}
}
}

Related

Android: Can't toggle MenuItem icon

I'm tring to change a menuitem icon, but the icon is not being changed.
Here is how I find the MenuItem (works fine):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_location_actions, menu);
mActionLocation = menu.findItem(R.id.action_location);
return super.onCreateOptionsMenu(menu);
}
Method UpdateIcon: I took a screenshot so you can see (in the red frame) that the images have been found by the system.
The mActionLocation is a MenuItem which is initialized before this Method is called and is not null.
Anyone has an idea?
UPDATE (solution by help from #vinitius)
The UpdateIcon method:
private void UpdateIcon(boolean locationOn) {
mActionLocation = locationOn; // mActionLocation is a global boolean
invalidateOptionsMenu();
}
The onPrepareOptionsMenu override:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Toggle location icon
if (mActionLocation) {
menu.findItem(R.id.action_location).setIcon(R.drawable.ic_location_on_white_24dp);
} else {
menu.findItem(R.id.action_location).setIcon(R.drawable.ic_location_off_white_24dp);
}
return super.onPrepareOptionsMenu(menu);
}
You need to use onPrepareOptionsMenu(Menu menu):
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.
Inside this method, retrieve your item as you do in onCreateOtionsMenu and check whether your gps in os or not to modify your item icon.

How to display icons in actionbar and how to handle click events

Trying to add actionar icons to actionbar and i'm using support library here is link i'm following developerspage
and here is my code to implement it
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == R.id.toggle) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
return true;
}
and menu.xml is
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/toggle"
android:icon="#drawable/menu"
android:orderInCategory="100"
android:title="menutoggle"/>
In your xml, you can include this in each item you want to be clickable:
android:onClick="aRandomMethod"
Then in your activity, you must implement a method that supports the click, in this case, a method called aRandomMethod, like this:
public void aRandomMethod(MenuItem item){
/// stuff to do when you click in the button
}
If you want your icon to be always displayed in the action bar, add:
android:showAsAction="always"
The onOptionsItemSelected method that you have in your code is when you have the settings icon (three small dots) and then a submenu in the icon; is to handle the submenu.

Android ActionBar Search Field

I have a standard search EditText in my ActionBar, triggered by a touch on a little magnifying glass icon. I can cache the user's search string using the OnQueryTextListener. I want to put that string back into the EditText when the user touches the icon a second time.
I'm using ABS (soon to abandon), targeting 8-19.
How can I do this?
With a normal action bar (you'll have to find the different variation with ABS, not sure if my answer will apply completely with that library).
SearchView is a widget that can be inflated in the options menu, so what I do is inflate a menu using an XML containing an action view:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_user"
android:actionViewClass="android.widget.SearchView"
android:icon="#drawable/magnifing_glass"
android:showAsAction="ifRoom|collapseActionView"
android:title="Search Users"/>
</menu>
Next, when inflating the action view, set your listeners, and use a global variable to save the previous search.
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
MenuItem search = menu.findItem(R.id.search_user);
//Keep a global variable of this so you can set it within the next listener
SearchView user_search = (SearchView) search.getActionView();
user_search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
global_variable = query;
return true;
}
#Override
public boolean onQueryTextChange(String text) {
return true;
}
});
Finally, in a second listener, set the text of the global variable to your previous query when the action view expands.
//This is set on the menu item
search.setOnActionExpandListener(new OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
user_search.setQuery(global_variable, false);
return true; // Return true to expand action view
}
});

Custom pull-down from ActionBarSherlock Action Item

I'm creating a universal app compatible with Froyo upwards, so I'm using the brilliant ActionBarSherlock. I wish to create submenu pulldowns from Action Items in the ActionBar that includes icons and text in the rows. There are a few threads that ask similar questions but I got no where with trying to implement them. I've tried Spinners but I need API 8 compatibility, so then I tried IcsSpinner in the Sherlock lib but Jake had advised someone else not to rely on it in case the lib changed. I tried a custom ActionProvider to mimic the ShareActionProvider but I found it too complicated:
This image shows exactly what I want but I could not get it to work with my app. My code is as follows:
public class AddDocActionProvider extends ActionProvider {
private Context mContext;
public AddDocActionProvider(Context context) {
super(context);
mContext = context;
}
#Override
public View onCreateActionView() {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(
R.layout.actionbar_new_doc_action_provider, null);
return view;
}
#Override
public boolean hasSubMenu() {
return true;
}
#Override
public void onPrepareSubMenu(SubMenu subMenu) {
// loop was here calling
subMenu.add(0, id, 0, "Type 1")
.setIcon(R.drawable.type_1)
.setOnMenuItemClickListener(mOnMenuItemClickListener);
// added type 2, 3, etc
}
#Override
public boolean onPerformDefaultAction() {
// This is called if the host menu item placed in the overflow menu of the
// action bar is clicked and the host activity did not handle the click.
return true;
}
My SherlockFragmentActivity had this code:
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
MenuItem newDoc = menu.add(0, MENU_ADD_DOC, 0, "New Document");
newDoc.setVisible(!isPhoneShowingStorageList);
newDoc.setIcon(R.drawable.dark_content_new);
newDoc.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
mNewDocActionProvider = new AddDocActionProvider(getSupportActionBar().getThemedContext());
newDoc.setActionProvider(mNewDocActionProvider);
}
I don't see the submenu and it also crashes on froyo phones when the actionbar invalidates.
Other threads I've looked at:
How to create a custom Pulldown in the Honeycomb ActionBar?
How to add a Dropdown item on the action bar
Custom drop-down from action item (actionbarsherlock)
While this isn't necessarily an ActionBarSherlock specific issue, frankly I can't believe something this easy should be so complicated to implement in the standard Action Bar. Any help would be greatly appreciated.
UPDATE:
Using XML rather than code added the icons for me:
<item
android:id="#+id/menu_new_doc"
android:icon="#drawable/dark_content_new"
android:showAsAction="always"
android:title="New Document">
<menu>
<item
android:id="#+id/word2010"
android:icon="#drawable/doc"
android:title="Word 2010"/>
<item
android:id="#+id/excel2010"
android:icon="#drawable/excel"
android:title="Excel 2010"/>
</menu>
</item>
So in order to dynamically had the submenus, I had to do this:
MenuItem newDoc = menu.findItem(R.id.menu_new_doc);
SubMenu subMenu = newDoc.getSubMenu();
subMenu.clear();
for (/* loop */) {
MenuItem subMenuItem = subMenu.add(0, hash, 0, fileType.GetDescription());
subMenuItem.setIcon(R.drawable.doc);
}
i think you should set the icon to the newly added Item to the subMenu. Example :
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu subMenu = menu.addSubMenu("Add");
subMenu.add("Add Subitem 1").setIcon(R.drawable.ic_action_add1);
subMenu.add("Add Subitem 2").setIcon(R.drawable.ic_action_add2);
MenuItem subMenu1Item = subMenu.getItem();
subMenu1Item.setIcon(R.drawable.ic_action_add);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}

Android: Append not shown ActionItems to Icon instead of Context Menu

I found an example how to use a context menu with actionBar. This example waits for clicks on the phones menu button. But I want to have it appended to the icon or better the activity name. thanks
public class menu extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_fragen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
..
After the logo and title comes an drop down menu indicator.
That has nothing to do with onCreateOptionsMenu() or onOptionsItemSelected(). To set up that Spinner:
Get your ActionBar via getActionBar()
Call setNavigationMode(ActionBar.NAVIGATION_MODE_LIST) on the ActionBar
Call setListNavigationCallbacks() on the ActionBar with your SpinnerAdapter and a listener object to be notified when the user makes a selection change in the Spinner

Categories

Resources