CheckableOptionsMenu

Hi there,

I am just a Android beginner...or advanced beginner. In this stadium of learning programming, you want to try different code examples to learn how to get more flexible. So, please note, this code is not a superman-programmers-code. If there is anything wrong, or could be made better (I think it could), please let me know. Thanks...

Sometimes a special optionsMenu is needed. In my case, I wanted an optionsMenu with some check-boxes as menu-items. There was no possibility to get some kind of options menu like this. But after a long time of search, I found an excelent code example from the "Code Project" web page:

http://www.codeproject.com/Articles/173121/Android-Menus-My-Way

The code is done by WJFrancis, many props to this great developer http://www.codeproject.com/Members/wjfrancis


I took this code and modified it a little bit, to get the result I wanted. Of course, my modification is just a simple one and I think there is a lot of improvement possible, but it works very well for my needs. To work with this modifications, I want to give you a short example of how to use these classes:

Put the resources into your appendent folders and start programming. Note that you don´t have to implement the methods onCreateOptionsMenu and onOptionsItemSelected.





1. ) In Your Activity, first implement an OnMenuItemSelectedListener, to get all actions you will do with the menuItems. Then, initialize the CheckableOptionsMenu:


public class DemoMenu extends Activity implements
        OnMenuItemSelectedListener {

    private CustomMenu mCheckableMenu;
    private static final int MENU_ITEM_1 = 1;
    private static final int MENU_ITEM_2 = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        init();
   
}
    private void init() {

        mCheckableMenu = new CustomMenu(this, this, getLayoutInflater()); //params: (Context,OnMenuItemSelectedListener,LayoutInflater)
        mCheckableMenu.setHideOnSelect(true); // if a menuItem is selected, hide the menu
        mCheckableMenu.setItemsPerLineInPortraitOrientation(4); //maximum of items per line (portrait). if you got more items, then the line will break down
        mCheckableMenu.setItemsPerLineInLandscapeOrientation(8); //same in landscape mode

        loadMenuItems();//create menu items
    
}


2.) Next, implement some keyEvents for showing or hiding your menu:

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            doMenu();
            return true;
        }

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            if (mCheckableMenu.isShowing()) {
                mCheckableMenu.hide();
            }
        }
        return super.onKeyDown(keyCode, event);
    }



3.) After that, you could start to create your menuItems. At this part for example, I modified the methods, to get a little bit closer to the native way of creating an optionsMenu(only a litlle bit :) )
    You have to load the items to an ArrayList<CustomMenuItem>


        private void loadMenuItems() {

        ArrayList<CustomMenuItem> menuItems = new ArrayList<CustomMenuItem>();

        CustomMenuItem cmi = new CustomMenuItem();
        String itemOneCaption = getString(R.string.item_one_text);
        cmi.configureItem(itemOneCaption, R.drawable.item_one_pic, 1);
//if you choose this part of overloaded method, you are creating a standard menu item(not checkable)
        menuItems.add(cmi);

        cmi = new CustomMenuItem();
        String itemTwoCaption = getString(R.string.item_two_text);
        cmi.configureItem(itemTwoCaption, 2);
//if you choose this part, you will get a checkable menu item. A checkable menu item has a line under the text, that will turn to
                                                                      
// green if it is checked. Note that you could not load a imageResource for this item
        cmi.setCheckableResourceParams(
                CustomMenuItem.SET_CHECKABLE_EQUAL_TO_CAPTION, 20, 50);
//here you could set the size behavior. width, height and margin to the caption. More details on                                                                                                                                         //method description
        menuItems.add(cmi);

        if (!mCheckableMenu.isShowing())
            try {
                mCheckableMenu.setMenuItems(menuItems);

            } catch (Exception e) {

                Toast.makeText(DemoMenu.this,
                        R.string.create_checkable_menu_error,
                        Toast.LENGTH_SHORT).show();
            }
    }


4. Now implement the behavior of what will happen when pressing menuButton

      
    private void doMenu() {
        if (mCheckableMenu.isShowing()) {

            mCheckableMenu.hide();
        } else {

            mCheckableMenu.show();

        }
    }


5. Finally, override MenuItemSelectedEvent:


            @Override
    public void MenuItemSelectedEvent(CustomMenuItem selection) {
        // TODO Auto-generated method stub
        switch (selection.getId()) {


        case (MENU_ITEM_1):

            Toast.makeText(DemoMenu.this,
^                        "MENU_ITEM_1 pressed",
                           Toast.LENGTH_SHORT).show();

            break;

        case (MENU_ITEM_2):

            if (selection.isChecked()) {
                Toast.makeText(DemoMenu.this,
                        "MENU_ITEM2 CHECKED:" + selection.isChecked(),
                        Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(DemoMenu.this,
                        "MENU_ITEM2 UNCHECKED:" + selection.isChecked(),
                        Toast.LENGTH_SHORT).show();
            }

            break;

        }
    }



These are the base options of what you could do with CheckableOptionsMenu. For more details, see source code descriptions. It would give me a great pleasure if you modify this code if you got good ideas and let other programmers know. Maybe at www.stackoverflow.com .....peace


ċ
Stefan Hennemann,
18.11.2012, 09:13
Comments