Search code examples
androidmenuoptionmenu

Android:How To use Sub_menu category in menu options


In My Activity i place a Option Menu like Text Options when the user click the text option an another option menu(Sub_menu) want to be load with text Colour and text size adjustment how can we do this in Android Plz help i am New To Android


Solution

  • Have your menu_activity.xml have all the menu item you want. If you don't want to show a menu item initially then set its visibility to false. android:visible="false"

    <item
        android:id="@+id/text_options"
        android:orderInCategory="2"
        android:title="@string/text_options"
        app:showAsAction="ifRoom"/>
    <item
        android:id="@+id/text_color"
        android:orderInCategory="3"
        android:title="@string/text_color"
        android:visible="false"
        app:showAsAction="ifRoom"/>       
    <item
        android:id="@+id/text_size"
        android:orderInCategory="4"
        android:title="@string/text_size"
        android:visible="false"
        app:showAsAction="ifRoom"/>      
    

    In your Activity have the following methods:

    private boolean isShown = true;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_activity, menu);
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.findItem(R.id.text_options).setVisible(!isShown);
        menu.findItem(R.id.text_color).setVisible(isShown);
        menu.findItem(R.id.text_size).setVisible(isShown);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.text_options:
                hideMenu();
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void hideMenu() {
        isShown = false;
        invalidateOptionsMenu();
    }
    
    public void showMenu() {
        isShown = true;
        invalidateOptionsMenu();
    }
    

    Once you call invalidateOptionsMenu(), the menu list will be recalculated, i.e, onPrepareOptionsMenu() will be called. Modify this a bit and you can achieve what you are looking for.

    UPDATE:

    If you want to add a sub_menu, then modify your menu_activity.xml to look like below:

    You can add a submenu to an item in any menu (except a submenu) by adding a element as the child of an . Submenus are useful when your application has a lot of functions that can be organized into topics, like items in a PC application's menu bar (File, Edit, View, etc.). For example:

     <?xml version="1.0" encoding="utf-8"?>
     <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/file"
              android:title="@string/file" >
            <!-- "file" submenu -->
            <menu>
                <item android:id="@+id/create_new"
                      android:title="@string/create_new" />
                <item android:id="@+id/open"
                      android:title="@string/open" />
            </menu>
        </item>
    </menu> To use the menu in your activity, you need to inflate the menu resource (convert the XML resource into a programmable object)
    

    using MenuInflater.inflate(). In the following sections, you'll see how to inflate a menu for each menu type.

    More info here: http://developer.android.com/guide/topics/ui/menus.html