So I’ve created an Activity subclass called CustomTitlebarActivity. Essentially, each main activity in my app will have a custom titlebar with many common features such as a Home button, a title, a search button, etc. In my current implementation, I am still explicitly using an include statement in the layout XML for each CustomTitlebarActivity:
<include layout="@layout/titlebar" />
It seems natural that I should be able to do this within CustomTitlebarActivity. I have two questions: What code can replace this include tag, and where should I put the code? (My first instinct would be to put it in CustomTitlebarActivity’s setContentView method.)
On a related note, I would appreciate insight into better ways to reuse android UI code (even if, per se, the titlebars need to vary slightly between activities.)
Personally, I’d probably write my
Activitysubclass to alwayssetContentViewto a layout file containing a verticalfill_parentLinearLayoutcontaining only my title bar:-Then I’d define an abstract
getContentAreaLayoutId()method inCustomTitlebarActivitythat returns the layoutIDof the content below the titlebar for each subclass; the baseonCreate()ofCustomTitlebarActivitywould then just callAlternatively, you could have your abstract method for getting the content area return a
Viewrather than anint, giving you more flexibility to construct your views dynamically (but forcing you to inflate them yourself in the simple just dump this XML layout here case).