Home > DeveloperSection > Articles > Leveraging action bar in Android

Leveraging action bar in Android


Android Android 
Ratings:
0 Comment(s)
 1093  View(s)
Rate this:

Leveraging action bar in Android

 

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.

 

Pre-Requisites:

1.       Ellipse SDK

2.       Android SDK

3.       ADT plugin

Or Android Studio 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

·         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 attribute:

 

 

        <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" />

 

 

 

 

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);

 

 

 

 

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.

 

 

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.

 

 

 

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

 


Don't want to miss updates? Please click the below button!

Follow MindStick