---
title: "Leveraging action bar in Android"  
description: "Hi everyone, we are going to leverage the functionalities of Action bar in android here."  
author: "Anonymous User"  
published: 2015-02-16  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1582/leveraging-action-bar-in-android  
category: "android"  
tags: ["android"]  
reading_time: 4 minutes  

---

# Leveraging action bar in Android

In my last post, we learn how to deal with fragments : **Fragment [Implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) in Android.** Now here we implement some basic characteristics of the action bar in android\

The action bar was introduced to the SDK in Android 3.0 (API Level 11), but was back-ported to earlier versions via the ActionBarActivity within the AppCompat [component](https://www.mindstick.com/articles/12262/learn-how-to-make-a-component-in-magento-2) of the Android SupportLibrary. Using ActionBarActivity, along with the included styles and [resources](https://www.mindstick.com/interview/1265/what-are-the-different-types-of-resources-through-which-cold-fusion-can-communicate) from AppCompat,we can put an action bar into any [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) [targeting](https://yourviews.mindstick.com/view/86047/niche-seo-targeting-specialized-audiences-in-2023) Android 2.1 and later.

## Pre-Requisites:

1. Ellipse SDK

2. Android SDK

3. ADT plugin

Or [Android Studio](https://www.mindstick.com/blog/10942/android-studio-vs-eclipse) and a compatible version of JAVA SDK

Install and configure the above utilities.

Now create a new Android project namely “**ActionBarApplicationTest**”.

**Implementation objective:**

We are going to implement the following basic characteristics of Action bar:

· Enabling Split in Action bar

· Enable Home and Up [Navigation](https://www.mindstick.com/blog/62/different-navigation-in-asp-dot-net)

· Create titles and sub-titles

· Create custom views in Action bar

## Enabling Split action bar:

Android employs the **split navigation mode**, in which the action items move to a secondary bar at the bottom of the screen when the screen is “narrow,” meaning there is less room to display everything in a single action bar. This option will be useful when you want to display the action items at the bottom of the screen by leaving some space on the title bar

This feature can be enabled only statically for an activity inside [AndroidManifest.xml](https://www.mindstick.com/interview/12768/what-is-the-androidmanifest-xml) by setting the splitActionBarWhenNarrow flag on the android:uiOptions [attribute](https://www.mindstick.com/blog/61/ado-dot-net-object-model-and-attributes):

```
 <activity            android:name=".MainActivity"
            android:label="@string/app_name"
            android:uiOptions="splitActionBarWhenNarrow">
            <!-- To support below api level 14 -->
            <meta-data android:name="android.support.UI_OPTIONS"
                android:value="splitActionBarWhenNarrow" />
```

![Leveraging action bar in Android](https://www.mindstick.com/mindstickarticle/80e61e3e-f35d-4d52-9c3d-de3524747253/images/ff053df5-b07e-4354-bb1a-77bce4701390.png)

## Home/ Up Button:

The upper-left corner of the action bar is the area designated for the **Home/Up button**. By default, this section displays the application’s icon and is not interactive. Many of the common navigation paradigms that Android applications implement involve this button being used to move back through previous screens or displaying menus.

## The following code snippet enables the Home/Up functionality in the app

```
    //Enables taps on the home logo
        getSupportActionBar().setHomeButtonEnabled(true);
        //Display home with the "up" arrow indicator
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
```

![Leveraging action bar in Android](https://www.mindstick.com/mindstickarticle/80e61e3e-f35d-4d52-9c3d-de3524747253/images/bb6d8766-ac23-467d-a4b2-601b46dd2f4f.png)

## Title/Sub-title:

The text area to the right of the Home/Up button is where the action bar displays a title and optional subtitle when in standard navigation mode. By default, the **android:label** value from the <application> or <activity> element in AndroidManifest.xml will be displayed as the title with no subtitle. The following code snipped allows an activity to customize both of these

```
        //Set the title text        getSupportActionBar().setTitle("Android Title");        //Set the sub title text        getSupportActionBar().setSubtitle("Android Sub-title");
```

If you want to hide the title completely, you can call these same methods and pass in either null or the [empty string](https://www.mindstick.com/forum/12697/json-stringify-is-sending-empty-string).

![Leveraging action bar in Android](https://www.mindstick.com/mindstickarticle/80e61e3e-f35d-4d52-9c3d-de3524747253/images/165d1a52-3e82-456a-8ae0-b297f2ded0dd.png)

## Custom Views:

The action bar supports placement of a custom view along the right side of the title area. This can be any view subclass that you would like to display. In common practice, it often makes sense to eliminate the title views if you would like to apply a custom view instead for real estate reasons.

## The following snippet places an ImageView displaying the application icon in place of the titles:

```
       //clear the title text
        getSupportActionBar().setTitle(null);
        //clear the subtitle text
        getSupportActionBar().setSubtitle(null);
        //set custom view
        ImageView iv = new ImageView(this);
        iv.setImageResource(R.drawable.ic_launcher);
        getSupportActionBar().setCustomView(iv,new           ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        //Enabling custom view display        getSupportActionBar().setDisplayShowCustomEnabled(true);
```

· An additional display option must be set to enable a custom view via **setDisplayShowCustomEnabled(true)**, and then we call setCustomView() to apply the view we want shown.

· This method can be called with or without passing discrete layout parameters; if none are passed, typically the view is set to horizontally wrap content and vertically match the parent.

![Leveraging action bar in Android](https://www.mindstick.com/mindstickarticle/80e61e3e-f35d-4d52-9c3d-de3524747253/images/121847e0-cab9-4e6b-9198-50f498406954.png)

Next, we are going to learn how to implement tab and list navigations in Action bar:

**Leveraging Action bar in android : Tab Navigation\
Leveraging Action bar in android : List Navigation**

Thanks for reading this post.

Happy Coding!! J

---

Original Source: https://www.mindstick.com/articles/1582/leveraging-action-bar-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
