Action bar: Best practice for older Android versions Action bar: Best practice for older Android versions android android

Action bar: Best practice for older Android versions


You might want to checkout the ActionBarSherlock. The source code is available here. It will give you an idea of how it is used along with fragments. It will give you some idea on how to proceed.

There is an android compatibility library which google created to use some of the new API's they created for honeycomb on lower version such as 1.5 and above. It can be found in the android sdk folder like E:\Program Files\Android\android-sdk-windows\android-compatibility\v4\android-support-v4.jar. If you don not have this jar you will need to download it using the avd manager.


My personal preference is to use AndroidBarSherlock. It's an excellent package and performs very well. If you want to avoid fragments, you can also use Android-ActionBar. The only thing about the android-actionbar is that you'll have to add the actionbar to each of your layouts.


What I've done is added an xml layout file called titlebar.xml, which looks something like:

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android" style="@style/FullWidth">    <RelativeLayout style="@style/FullWidth">        <ImageView style="@style/WrapContent" android:src="@drawable/mylogo" android:layout_centerVertical="true" android:id="@+id/imageViewIcon" />        <TextView style="@style/Title" android:layout_toRightOf="@+id/imageViewIcon" android:layout_toLeftOf="@+id/buttonSomeButton" android:id="@+id/textViewAppName"  android:text="@string/appName"/>        <TextView style="@style/SubTitle" android:text="@string/empty" android:layout_marginLeft="3dip" android:layout_toRightOf="@+id/imageViewIcon" android:layout_toLeftOf="@+id/buttonSomeButton" android:layout_below="@+id/textViewAppName" android:id="@+id/textViewSubtitle" />        <Button style="@style/NormalButton" android:id="@+id/buttonSomeButton" android:drawableRight="@drawable/button_image" android:layout_centerVertical="true" android:layout_alignParentTop="true" android:layout_alignParentRight="true" />    </RelativeLayout></merge>

And then I have created a control that extends LinearLayout called TitleBar.java, that looks something like:

package com.sample.ui;public class TitleBar extends LinearLayout {    public TitleBar(Context context, AttributeSet attrs) {        super(context, attrs);            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        inflater.inflate(R.layout.titlebar, this);    }}

You can hook up the buttons all within this class. If you need to have custom button handlers, you can create set methods in this class. It keeps it all nicely contained in a single control.

To use it in any XML file where I want to include this titlebar, to do so like:

<com.sample.ui.TitleBar style="@style/FullWidth" />

That works well for my purposes, and it is compatible with 2.1 and above. I haven't tested with below 2.1, but I don't see why it wouldn't work with previous versions.