How to create a custom titlebar


SDK Version: 
M3

If you got sick and tired of the default style/behavior of the title bar in your apps, or just need something different, than here is a little snippet for You.

It has 2 textviews, and a progress bar in it.
It needs 2 parameters: String left, String right, these are the texts that will be displayed. Usually, the left one is the app name, the right one is dynamic. So if the right one is longer than 20 characters, it will be truncated.

Here is the layout:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout android:id="@+id/RelativeLayout01&quot;
  3.         android:layout_width="fill_parent" android:layout_height="fill_parent"
  4.         xmlns:android="http://schemas.android.com/apk/res/android">
  5.         <TextView android:layout_width="wrap_content"
  6.                 android:layout_height="wrap_content" android:id="@+id/titleTvLeft"
  7.                 android:text="left"></TextView>
  8.         <TextView android:layout_width="wrap_content"
  9.                 android:layout_height="wrap_content" android:id="@+id/titleTvRight"
  10.                 android:text="right" android:layout_alignParentRight="true"></TextView>
  11.  
  12.         <ProgressBar android:id="@+id/leadProgressBar&quot;
  13.                 style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content"
  14.                 android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/titleTvLeft" android:paddingLeft="3dip"></ProgressBar>
  15. </RelativeLayout>

Here is the code:

  1. boolean customTitleSupported;
  2. public void onCreate(Bundle savedInstanceState) {
  3.         super.onCreate(savedInstanceState);
  4.  
  5.         //check if custom title is supported BEFORE setting the content view!
  6.         customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
  7.        
  8.         //set contentview
  9.         setContentView(R.layout.mainscreen);
  10.  
  11.         //set custom titlebar
  12.         customTitleBar(getText(R.string.app_name).toString(), getText(
  13.                 R.string.title_main_menu).toString());
  14. }
  15.  
  16. public void customTitleBar(String left, String right) {
  17.         if (right.length() > 20) right = right.substring(0, 20);
  18.         // set up custom title
  19.         if (customTitleSupported) {
  20.                 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
  21.                                 R.layout.customtitlebar);
  22.                 TextView titleTvLeft = (TextView) findViewById R.id.titleTvLeft);
  23.                 TextView titleTvRight = (TextView) findViewById(R.id.titleTvRight);
  24.  
  25.                 titleTvLeft.setText(left);
  26.                 titleTvRight.setText(right);
  27.  
  28.                 ProgressBar titleProgressBar;
  29.                 titleProgressBar = (ProgressBar) findViewById(R.id.leadProgressBar);
  30.  
  31.                 //hide the progress bar if it is not needed
  32.                 titleProgressBar.setVisibility(ProgressBar.GONE);
  33.         }
  34. }