How to create a custom titlebar
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:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout android:id="@+id/RelativeLayout01&qu
ot; - android:layout_width="fill_parent" android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.c
om/apk/res/android"> - <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/titleTvLeft"
- android:text="left"></TextView>
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:id="@+id/titleTvRight"
- android:text="right" android:layout_alignParentRight="true"></TextView>
- <ProgressBar android:id="@+id/leadProgressBar&quo
t; - style="?android:attr/progressBa
rStyleSmall" android:layout_width="wrap_content" - android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/titleTvLeft" android:paddingLeft="3dip"></ProgressBar>
- </RelativeLayout>
Here is the code:
- boolean customTitleSupported;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //check if custom title is supported BEFORE setting the content view!
- //set contentview
- setContentView(R.layout.mainscreen);
- //set custom titlebar
- customTitleBar(getText(R.string.app_name).toString(), getText(
- R.string.title_main_menu).toString());
- }
- if (right.length() > 20) right = right.substring(0, 20);
- // set up custom title
- if (customTitleSupported) {
- R.layout.customtitlebar);
- TextView titleTvLeft = (TextView) findViewById R.id.titleTvLeft);
- TextView titleTvRight = (TextView) findViewById(R.id.titleTvRight);
- titleTvLeft.setText(left);
- titleTvRight.setText(right);
- ProgressBar titleProgressBar;
- titleProgressBar = (ProgressBar) findViewById(R.id.leadProgressBar);
- //hide the progress bar if it is not needed
- titleProgressBar.setVisibility(ProgressBar.GONE);
- }
- }
New tutorials from Helloandroid
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 2 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 2 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 2 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 2 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 2 days ago
Poll
Useful resources
Android Development Projects
- simple android app just convert - repost by thorapps
- Android 4pics1word Game by yhassany
- Synchronize Faceebook and Twitter feeds by mahdidarwiche
- Fake bomb for Tablet (for airsoft / paintball games) by seanchiarot
- Trading application by TravisVB
- Android App - live footy (to complete started project) by dmaco
- Worksmart mobile application by Richabhatia2
- Chess APP for Web and Mobile by johnohiggs
- androidApp - repost by mmalselek
- Nonpublic project #4532466 by folababa



