Basics of Action Bars Using Android Studio 1.0


An Action Bar is a type of layout or animated graphic. It is a design element to be implemented in an Activity of any application. It is very certain and practical that when an Activity is launched by the user then it must contain the title of that Activity (a basic component of an application) not in the text form but also in the animated or symbolic form.

While an activity is loading the user must become bored. However it provides several user interface features that make your application immediately familiar to the user by offering consistency among other Android applications in the device.

Key Functions of an Action Bar

  • A dedicated space for giving your application an identity and indicating the user's location in the application

  • Access to important actions in a predictable way (for example "Search")

  • Support for navigation and view switching ("with tabs" or "dropdown lists")

Setting up the Action Bar

As in the most basic form, the Action Bar displays the title for the activity and the app icon on the left of the Action Bar strip as we can see. Even in this simple form, the Action Bar is useful for all activities to inform users about where they are and to maintain a consistent identity for your application.

Basically for setting up an Action Bar it is necessary for our application to use an Activity theme that enables the Action Bar feature because these themes will define the look of the Action Bar strip. The selection of the themes depends upon which version of Android is the lowest supported by our application.

Support Android 3.0 and Above Only

When it comes to the Action Bars in the Android then one thing that one should know is that an Action Bar is supported beginning with version 3.0 of API level 11 in all activities. The Action Bar is included in all activities  that use the Theme.Holo theme and one of its descendants that is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or "greater".

Adding Action Bar

First we must set the attribute to 11 or higher in the Android manifest.xml file if you are using any other IDE than Android Studio and in Android Studio 1.0 it is set to be in build.Gradle as shown in the figure.

graadle dependencies

However we are using Android Studio that's why I am not showing the Manifest file. This is taken over by Gradle in this IDE.

You can choose any theme. Basically Theme.Holo is the default and parent theme of all the themes. If you want to set a custom theme then the Holo theme must be the parent theme. 

Support Version Below 3.0

  • Let us suppose we use Android version 2.1 then adding the Action Bar requires that you include the Android Support library in the application we are developing. 

  • Integrate the support library with our project.

  • Update the activity so that it will extend ActionBarActivity. For example: 
  1. public class MainActivity extends ActionBarActivity {.....}  

We must update the <application> element or individual <activity> elements to use one of the Theme.AppCompat themes. For example let us look at the following line of code:

  1. <activity android : theme="@styles/theme.AppCompat.Light"...>  
And remember to set the API level to 7 or higher.

Adding Action to Action Bar

The Action Bar allows programmers to add buttons and animatory title as well for the most important action and convenient items relating to the applications current context. Those appear directly in the Action Bar with an icon or text and is known as action buttons. Actions that can't fit in the Action Bar are hidden in most cases.

For example in some Action Bars there is a search symbol, a magnificent glass symbol that is more precise, that the user must click on to begin a search,

First specify the Actions in XML by adding an <item> element for each item that we want to include in an Action Bar.
  1. <menu xmlns:android=""  
  2. xmlns:app=""  
  3. xmlns:tools="" tools:context=".ActionActivity">  
  4.    <item android:id="@+id/action_search"  
  5.       android:icon="@drawable/ic_action_search"  
  6.       android:title="@string/action_search"  
  7.       android:showAsAction="ifRoom" />  
  8.    // Settings, should always be in the overflow   
  9.    <item android:id="@+id/action_settings"  
  10.       android:title="@string/action_settings"  
  11.       android:showAsAction="never" />  
  13. </menu>  
The icon attribute requires a resource id for an image. The name @drawable must be the name of the bitmap image in the application.

What If one is using a Support library

If a programmer is building an application for a version lower than 3.0 then the activity_actionbar.xml will look like this. 

  1.  <item android:id="@+id/action_search"  
  2.    android:icon="@drawable/ic_action_search"  
  3.    android:title="@string/action_search"  
  4.    myapp:showAsAction="ifRoom" />  
  5.    ...  
Add the Action Button to the Response
  1. package com.example.gkumar.actionbars;  
  3. import;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  8. public class ActionActivity extends ActionBarActivity {  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_action);  
  14.     }  
  16.     @Override  
  17.     public boolean onCreateOptionsMenu(Menu menu) {  
  18.         // Inflate the menu; this adds items to the action bar if it is present.  
  19.         getMenuInflater().inflate(, menu);  
  20.         return true;  
  21.     }  
  23.     @Override  
  24.     public boolean onOptionsItemSelected(MenuItem item) {  
  25.         // Handle action bar item clicks here. The action bar will  
  26.         // automatically handle clicks on the Home/Up button, so long  
  27.         // as you specify a parent activity in AndroidManifest.xml.  
  28.         switch (item.getItemId()) {  
  29.             case  
  30.                 openSearch();  
  31.                 return true;  
  32.             case  
  33.                 openSettings();  
  34.                 return true;  
  35.             default:  
  37.                 return super.onOptionsItemSelected(item);  
  38.         }  
  39.     }  
  40. }  

This article explained the basics of an Action Bars. However we have written some part of the entire code that will be a clue for beginners. Think of your Android phone without an Action Bar. That would be boring. Styling and customizing the Action Bars will be shown in the next article. 


Up Next
    Ebook Download
    View all
    View all