Android - add icon in actionbar

12,977

To control your menu inside a Fragment, you need to call this method:

setHasOptionsMenu(true);  

inside onCreateView method. Then, you need the MenuInflater as follows:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem itemBlog = menu.add(Menu.NONE, // Group ID
                                 R.id.blog_item, // Item ID
                                 1, // Order
                                 R.string.blog_item); // Title
    itemBlog.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); // ShowAsAction
    itemBlog.setIcon(R.drawable.ic_action_blog); // Icon
    // add your item before calling the super method
    super.onCreateOptionsMenu(menu,inflater);
}    

I don't know if this is the case, however if you use the AppCompat library, you should do as follows:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem itemBlog = menu.add(Menu.NONE, // Group ID
                                 R.id.action_blog, // Item ID
                                 101, // Order
                                 "Blog"); // Title
    // To showAsAction attribute, use MenuItemCompat (set to always)
    MenuItemCompat.setShowAsAction(itemBlog, MenuItem.SHOW_AS_ACTION_ALWAYS);
    itemBlog.setIcon(R.drawable.ic_action_blog);
    super.onCreateOptionsMenu(menu, inflater);
}

This works well.

Share:
12,977
Christian Giupponi
Author by

Christian Giupponi

Updated on August 12, 2022

Comments

  • Christian Giupponi
    Christian Giupponi over 1 year

    I need to add an icon at the top right of the menu in the ActionBar when an item is selected from the NavigationDrawer menu.

    So I have create a new file called for example 'blog' that extends Fragment and I have write in it the onCreateOptionsMenu method that should manage the menu action bar right?

    This is my code:

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class Blog extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.blog, container, false);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu items for use in the action bar
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.blog_menu, menu);
            return super.onCreateOptionsMenu(menu);
        }
    }
    

    Android studio write in red this line of code:

    MenuInflater inflater = getMenuInflater();
    

    so I think that it is wrong but I do not understand what is the problem...any help?

  • Blo
    Blo about 10 years
    @ChristianGiupponi I've just tried this snippnet, it works.. What are you currently using AppCompat, ABSherlock or nothing for your ActionBar?
  • Christian Giupponi
    Christian Giupponi about 10 years
    I have started a new project and it works, maybe I have removed something in the previous project, thanks!