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. }

Comments

This one is really nice tutorial regarding creating custom titlebar. I get good help from this tutorial. Thanks
Regards
crazy taxi game

This is exactly the type of tutorial I was looking for. My coding was slightly wrong though, however your code put me back on the right track. I wish there were more sites like this on the web. Thanks a bunch :)
84 Inch Shower Curtain

Does this code work on the Droid X2?

A IRS wage garnishment is a judgment placed by the IRS which requires an employer to withhold money from an employee’s wages for the payment of a tax debt.

Thanks for posting this article. I am definitely tired of struggling to find relevant and intelligent commentary on this subject. Everyone nowadays seem to go to extremes to either drive home their viewpoint or suggest that everybody else in the globe is wrong. Thanks for your concise and relevant insight. Mike Round // Free Online Games

I really believe you will do much better in the future I appreciate everything you have added to my knowledge base.Admiring the time and effort you put into your blog and detailed information you offer! David Harris // pest control london

This is such an informative article and very clearly written. Every single thought and idea is direct to the point. Perfectly laid out. Thank you for taking your time sharing this to you readers. Gary Malone // diseño web valencia

Very interesting articles. Good work done
batik

Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging. Stan Redworth // Hotelbewertungen

Here is the layout:

<?xml version="1.0" encoding="utf-8"?>

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!
customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

//set contentview
setContentView(R.layout.mainscreen);

//set custom titlebar
customTitleBar(getText(R.string.app_name).toString(), getText(
R.string.title_main_menu).toString());
}

public void customTitleBar(String left, String right) {
if (right.length() > 20) right = right.substring(0, 20);
// set up custom title
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
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);
}
}
a b c d e f g h i j

=========================================================

ipad bag blog
Sutudeg Community
Education News

=========================================================

a visit b visit c visit d visit e visit f visit g visit h visit i visit j visit k visit l visit m visit n visit o visit p visit q visit r visit s visit t visit u visit v visit w visit x visit y visit z visit aa visit ab visit ac visit ad visit ae visit af visit ag visit ah visit ai visit aj visit ak visit al visit am visit an visit ao visit ap visit aq visit ar visit as visit at visit au visit av visit aw visit ax visit ay visit az visit ba visit bb visit bc visit bd visit be visit bf visit bg visit bh visit bi visit bk visit bl visit bm visit bn visit bo visit bp visit bq visit br visit bs visit bt visit bu visit bv visit bw visit bx visit by visit bz visit ca visit cb visit cc visit cd visit ce visit cf visit cg visit ch ci cj ck cl ccl cm cn co cp cq cr cs ct cu cv

=========================================================

I think youve made some truly interesting points. Not too many people would actually think about this the way you just did. Im really impressed that theres so much about this subject thats been uncovered and you did it so well with so much class. Good one you man! Really great stuff here. Helen West // tapeten

Good job! I have found many articles to read but you do a good thing. That is a boy. Thank you so much for sharing the delicious post. Expect your next article. Jack Freyer // crazy taxi

I really believe you will do much better in the future I appreciate everything you have added to my knowledge base.Admiring the time and effort you put into your blog and detailed information you offer! Ryan White // best ceiling fans

These facts are amazing . I was searching for at least 5 weaks and i didn't get the perfect answer. But after all i found from your site. thanks for posting such a interesting topic. Sean Hough // Relatiegeschenken

To tell you the teuth I was passing around and come across your site. It is wonderful. I mean as a content and design. I added you to my list and decided to spent the rest of the weekend browsing. Well done! Rob Edwards // hvac

Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging. Dan Grafton // Buitenspeelgoed

I admit I have not been on this blog in a long time however it was joy to find it again. It is such an important topic and ignored by so many even professionals! I thank you for helping to make people more aware of these issues. Just great stuff as per usual! Max Hazel // Ephesus Tours

attractive and I am happy to find so many good point here in the post writing is simply great thanks
van insurance

These kind of articles are always attractive and I am happy to find so many good point here in the post writing is simply great thanks for sharing. Brad Michaels // autel maxidas ds708

I like it very much because it has very helpful articles of various topics like different culture and the latest news. I am a googler and search on many topics. Miles Jordan // Termine Höinger SV

Thanks for this wonderful post and hoping to post more of this blog here. I am a newbie and your success is very much an inspiration for me. Blair Jenkins // Lanterns

I think I could disagree with the main ideas. I won't share it with my friends.. You should think of other ways to express your ideas. Tom Penn // Computer Repair Sherman Oaks

Nice one there is actually some great points on this post some of my associates will find this worthwhile will send them a link thanks Dez Peterson // Versicherungsmakler

I know so a great deal a lot more about this than I did previous to. Your blog seriously brought some items to light which i never would have believed about ahead of reading through it Pete Blackwell // Electrician Herts

I have to say this post was certainly informative and contains useful content for enthusiastic visitors. I will definitely bookmark this blog for future reference and further viewing. Thanks a bunch for sharing this with us! Harry Bishop // Training Courses Online

Thanks for the time eater it made waiting a breeze. Good job people Kristoff Jam // Game development blog

Thanks for sharing the information. I found the information very helpful. That's a awesome article you posted. I will come back to read some more. Lily Connely // online classified ads

Thanks particularly for the quickstart vids I was so excited after downloading the tools that you cannot imagine how disappointed I was to discover I couldn't get them to work! You came to my rescue and saved the day! Susie Francis // Makeup Styles

Thanks for posting this article. I am definitely tired of struggling to find relevant and intelligent commentary on this subject. Everyone nowadays seem to go to extremes to either drive home their viewpoint or suggest that everybody else in the globe is wrong. Thanks for your concise and relevant insight. Steven Bants // Geld verdienen Internet

Anything designed according to the users needs and comfort works greatly and has wide acceptance. Your is a nice idea in this regard. It is innovative and creative in nature. James Lawrence // soliver

It's so tough to encounter right information on the blog. I really loved reading this post. It has strengthen my faith more. You all do such a great job at such concepts. ..can't tell you how much I for one appreciate all you do. John Green // erectile dysfunction treatment

The post was able to express what it wants to convey to the readers. It has been a very effective approach which resulted to a profitable output for all who have been fortunate enough to come across it! James Hunt // watch hindi movies online

Excellent read I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch! Brian Margate // Fleshlight Videos & Reviews

Youre so cool! I dont think Ive read anything like this before. So good to find somebody with some original thoughts on this subject. Thanks for starting this up. This blog is something that is needed on the web someone with a little originality. Good job for bringing something new to the internet! Charlotte Cooper // Brea electrician

A very informative article and lots of really honest and forthright comments made! This certainly got me thinking a lot about this issue so thanks a lot for posting! Tim Whicher // Apple News

Considerably the article is in reality the greatest on this noteworthy topic. I agree with your conclusions and will eagerly look forward to your next updates. Saying thanks will not just be sufficient for the wonderful clarity in your writing. I will immediately grab your rss feed to stay privy of any updates! Tim Whicher // Apple News

I think I could disagree with the main ideas. I won't share it with my friends.. You should think of other ways to express your ideas. Skye Konah // Khosrow Semnani

I'm extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you modify it yourself? Anyway keep up the nice quality writing it’s rare to see a nice blog like this one these days.. David Hardy // Terrassenholz

I found the perfect place for my needs. Contains wonderful and useful messages. I have read most of them and has a lot of them. Alex Shaw // ezine software

Thats what i called awesome truly and informative information.. Good work done...Your post sounds great.. Very unique informations you've posted.. good job done.. Jason Walker // Shipping to Malaysia

I was very pleased to find this site. I wanted to thank you for this unique read. I definitely savoured all bits and pieces of it including all the comments and I have added you to my bookmark list to check out new articles you post. Aled Mutters // Speelgoed

I'm the same way I do my best to remain neutral. It's hard if you communicate with the person the other person dislikes then you fall out of favor with them! I simple can't dislike a person just because someone else does I just can't. Karl Jenkins // kinox.to

Thanks for making such a killer blog. I arrive on here all the time and am floored with the fresh information here! You are great! Patrick Shaw // hid kits

These kind of post are always inspiring and I prefer to read quality content so I happy to find many good point here in the post writing is simply great thank you for the post Terry Grayson // buy traffic

Firma opplysninger
Thanks for posting this article. I'm definitely tired of struggling to find relevant and intelligent comments on this topic. Everybody today seems to go to extremes to either drive home their views or suggest that anyone else in the world is wrong. Thank you for your accurate and relevant insights.

Thank you so much for the tutorial. I so know now, although still a little confused. My permission to try it.

If willing, I hope you can visit my site too:
Kalangkang
Digital Cameras Bestsellers And The Best Prices on the Most Computers Reviews

Hrmm that was weird my comment got eaten. Anyway I wanted to say that it's nice to know that someone else also mentioned this as I had trouble finding the same info elsewhere. This was the first place that told me the answer. Thanks. Terry Bishop // Forex Megadroid Review

I thought it was going to be some boring old post but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful. Will Murray // installe