Leveraging action bar in
In my last post, we learn how to deal with fragments : Fragment 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 of the Android SupportLibrary. Using ActionBarActivity, along
with the included styles and resources from AppCompat,we can put an action bar
into any application targeting Android 2.1 and later.
Or Android Studio and a compatible
version of JAVA SDK
Install and configure the above utilities.
Now create a new Android project namely “ActionBarApplicationTest”.
We are going to implement the following basic characteristics
of Action bar:
Enabling Split in Action bar
Enable Home and Up Navigation
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 by
setting the splitActionBarWhenNarrow
flag on the android:uiOptions
<!-- To support
below api level 14 -->
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
The following code snippet enables the Home/Up functionality
in the app
//Enables taps on
the home logo
with the "up" arrow indicator
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
//Set the sub
If you want to hide the title completely, you can call these
same methods and pass in either null or the empty string.
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
//set custom view
ImageView iv = new
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.
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