Creating Submenus and Checkable menu items in Android

July 26, 2012
By

In this article we will create an application with submenus and checkable menu items in Android.

This application is developed in Eclipse ( 3.7.2 )  the ADT plugin ( 20.0.2 ) and Android SDK ( R20.0.1)


1. Create new Android application project namely “SubmenuDemo”

Create new Android Application Project

Figure 1 : Create new Android Application Project


2. Design application launcher icon

Design Application Launcher

Figure 2 : Design Application Launcher


3. Create a blank activity

Create a blank activity

Figure 3 : Create a blank activity


4. Enter activity details

Enter Activity Details

Figure 4 : Enter Activity Details


5. res/values/strings.xml


<resources>

    <string name="app_name">SubmenuDemo</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>

    <string name="action1">Action1</string>
    <string name="action2">Action2</string>
    <string name="action3">Action3</string>
    <string name="action4">Action4</string>

    <string name="action4_sub1">Action4 Sub1</string>
    <string name="action4_sub2">Action4 Sub2</string>
    <string name="action4_sub3">Action4 Sub3</string>

    <string name="action3_sub1">Action3 Sub1</string>
    <string name="action3_sub2">Action3 Sub2</string>
    <string name="action3_sub3">Action3 Sub3</string>

</resources>


6. res/menu/activity_menu.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action1"
        android:title="@string/action1"
        android:orderInCategory="100"
        android:showAsAction="never" />

    <item android:id="@+id/action2"
        android:title="@string/action2"
        android:orderInCategory="100"
        android:showAsAction="never" />

    <item android:id="@+id/action3"
        android:title="@string/action3"
        android:orderInCategory="100"
        android:showAsAction="never" >

        <menu>
            <group
                android:checkableBehavior="single"
                >
                <item android:id="@+id/action3_sub1"
                    android:title="@string/action3_sub1"
                    android:orderInCategory="100"
                    android:showAsAction="never" />

                <item android:id="@+id/action3_sub2"
                    android:title="@string/action3_sub2"
                    android:orderInCategory="100"
                    android:showAsAction="never"/>

                <item android:id="@+id/action3_sub3"
                    android:title="@string/action3_sub3"
                    android:orderInCategory="100"
                    android:showAsAction="never" />
            </group>
        </menu>
    </item>

    <item android:id="@+id/action4"
        android:title="@string/action4"
        android:orderInCategory="100"
        android:showAsAction="never" >

        <menu>
            <group
                android:checkableBehavior="all"
            >
                <item android:id="@+id/action4_sub1"
                    android:title="@string/action4_sub1"
                    android:orderInCategory="100"
                    android:showAsAction="never" />

                <item android:id="@+id/action4_sub2"
                    android:title="@string/action4_sub2"
                    android:orderInCategory="100"
                    android:showAsAction="never"

                />

                <item android:id="@+id/action4_sub3"
                    android:title="@string/action4_sub3"
                    android:orderInCategory="100"
                    android:showAsAction="never" />
            </group>
        </menu>
    </item>
</menu>


7. src/in/wptrafficanalyzer/submenudemo/MainActivity.java


package in.wptrafficanalyzer.submenudemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.isChecked())
            item.setChecked(false);
        else
            item.setChecked(true);
        return true;
    }
}


8. Application in execution

Main Activity

Figure 5 : Main Activity

Checkable Menuitems in Single Selection Mode

Figure 6 : Checkable Menuitems in Single Selection Mode

Checkable Menuitems in Multiselection mode

Figure 7 : Checkable Menuitems in Multi Selection mode


9. Download


10. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , ,

2 Responses to Creating Submenus and Checkable menu items in Android

  1. Akhilesh Sharma on September 20, 2014 at 11:34 am

    Dear Sir,
    You are really doing superb work. Keep going..
    All the best wishes !!!

    And a HUGE thanks for help i got here and at other pages

  2. Vishal M. Vekariya on May 30, 2015 at 7:32 pm

    Can we have develop a sub menu in slider menu

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner