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
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 37 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 37 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 37 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 38 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 38 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN