How to change Menu Item icon from fragment?

Micro

How can I access my Menu from my fragment and then change the icon of one of the menu items there?

What I am doing is querying my local DB to see if a certain entry exists when the fragment is shown. If it does display a solid icon, if not, display an outlined icon.

Mike

In your fragments onCreate() method you can use setHasOptionsMenu(true) to allow your fragment to handle different menu items than it's root Activity. So you could do something like this in your fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

And then, you can override any of the menu life-cycle methods in your fragment:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_fragment, menu);
    // You can look up you menu item here and store it in a global variable by 
    // 'mMenuItem = menu.findItem(R.id.my_menu_item);'
}

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    MenuItem menuItem = menu.findItem(R.id.menu_item_to_change_icon_for); // You can change the state of the menu item here if you call getActivity().supportInvalidateOptionsMenu(); somewhere in your code
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    // Handle actions based on the id field.
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change the size of menu item icon in NavigationView?

From Dev

Windows 7: How to change context menu item icon?

From Dev

Icon for menu item from layout

From Dev

How to change the icon of a listview item

From Dev

How to change the icon of a listview item

From Dev

get drawable from Android Menu Item Icon

From Dev

android: menu item click event from fragment

From Dev

How to display menu item with icon and text in AppCompatActivity

From Dev

How to show drawing brush as a Menu item Icon

From Dev

change color of only ONE menu item icon in android

From Dev

How to delete item from Fragment

From Dev

Android menu item with icon

From Dev

How to change option menu icon in the action bar?

From Dev

How to change the menu icon of a Toolbar in AppCompat

From Dev

How to change Ubuntu Mate applications menu icon

From Dev

How to (easily) change Lubuntu applications menu icon

From Dev

Change the color of menu icon

From Dev

Selecting item of sliding tabs menu from navigation drawer with some fragment

From Dev

Accessing the same fragment from a button and menu item JAVA

From Dev

How change menu item to checked or unchecked?

From Dev

How to change a single menu item text size

From Dev

How to change active menu and submenu item style

From Dev

How to change item names in CKEditor "Format" menu?

From Dev

How to change the text color of context menu item

From Dev

How to change color of hovered menu item in jquery?

From Dev

How to change item names in CKEditor "Format" menu?

From Dev

How can I change the menu item style

From Dev

Change actionbar menu in fragment

From Dev

How to place checkbox icon to left of text in checkable menu item in android?

Related Related

HotTag

Archive