Android ActionBar and Option Menus - Androhub

Actionbar and Option Menus

Android ActionBar and Option Menus

What is the action bar?

The action bar (ActionBar) is located at the top of the activity. It can display the activity title, icon, actions which can be triggered, additional views and other interactive items. It can also be used for navigation in your application.

The action bar is enabled for devices which specify a target SDK of API version 11 or higher. It is possible to disable the action bar via the used theme, but the default Android themes have it enabled.

Applications with a target SDK version less than API 11 use the options menu if such a button is present on the device. The option menu is displayed if the user presses the Option button. The action bar is superior to the options menu, as the action bar is clearly visible, while the options menu is only shown on request. In case of the options menu, the user may not recognize that options are available in the application.

What is the option menus?

Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc.

We inflate the menu by calling the inflate() method of MenuInflater class. To perform event handling on menu items, you need to override onOptionsItemSelected() method of Activity class.

Note :  Both actionbar and option menus can be customized.

Example

In this example i will demonstrate you how to work on action bar and option menus and also custom action bar and option menus.

VIDEO DEMO

Let’s get start by creating a project in Eclipse IDE.

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)

2. Open your your AndroidManifest.xml file and make your “MainActivity” as Launcher activity and some extra stuffs like meta data that is going to be use in actionbar.

3. Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i made three buttons for different actions.

4. Now, add the following code in MainActivity.java. In this class we set logo, title, subtitle and implement actionbar option menus and do something on menu selecting and also showing and hiding on first button pressed.

5. Create a new layout file naming custom_actionbar_menu.xml for custom menu. In this layout we take one textview and one edittext according to menus that we want to display for demo i am taking two widgets only.

6. Now create a java file naming CustomMenu.java for custom menus that is started on button click from MainActivity.java and add the following code. In this activity i am going to show how to inflate custom menu over actionbar and implement click listeners over them.

7. For, custom actionbar create an new layout naming custom_actionbar.xml. In this layout i made one textview with an icon for custom actionbar title and icon.

8. Now, finally make a new java class for custom actionbar naming CustomActionBar.java and add the following code. In this class i am going to show how inflate custom actionbar and implement click listener on title.

9. Finally, you are all set and run the app and you will get the output as shown in video.

Thanks. :)

 

Post comment

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