How to create Contextual Action Mode Menu in Android
Context Menus provides more flexibility and action which affects the specific item or a context frame in an user interface.It is available for any kind of view like ListView or GridView.
There are two ways of using Context Menus.
Lets us begin programming for Contextual action Mode.There are two ways of using Context Menus.
- Floating Context Menu- visit here for more details.
- Contextual Action Mode- This is a system implementation of action mode which displays contextual action bar at the top of screen with action items.Contextual Action Mode is available at Android 3.0(API 11) or higher version otherwise you need to use Contextual Floating Menus.
- First open eclipse and start new project by selecting new-> file-> Android Application Project.
- Then fill the following details in the project window.
- Application Name-ContextualAction_Mode
- Project Name-ContextualAction_Mode
- Package Name- action_mode.contextual
- Build SDK-Android 4.1(API 14)
- and then click next and next and then finish.
- Now open activity_main.xml under menu folder in the project.
- In activity_main.xml of menu folder you will see the following lines in activity-main.xml .
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_settings"
android:title="@string/menu_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
- Just replace the above lines with these new lines in activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action1"
android:title="@string/Menu1"
/>
<item
android:id="@+id/action2"
android:title="@string/Menu2"
/>
<item
android:id="@+id/action3"
android:title="@string/Menu3"
/>
</menu>
- Now open activity_main.xml in layout folder of the project.
- In the activity_main.xml of layout folder you will see the following lines.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
- Just replace the above lines with these new lines in activity-main.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
- Now open strings.xml in values folder of the project.
- In strings.xml of values folder you will the following lines.
<resources>
<string name="app_name">ContextualAction_Mode</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resource