Thursday, August 25, 2011

Howto - Create a custom title

Dynamic title

By default, a view uses the app_name string value as its title. In our case, we want to show a mortgage rate supplier's name as the title of our rate selection dialog.

Layout XML
Declared in select_rate_dlg_title.xml, a <RelativeLayout> with a <TextView> as ...

<RelativeLayout>
<TextView
android:id="@+id/selectRateDlgTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/select_a_rate" >
</RelativeLayout>



In Java (i.e. Activity's onCreate method)
- Request the Window feature (i.e. Window.FEATURE_CUSTOM_TITLE ) before setting it.
- Set the Window feature to use the RelativeLayout defined above.
- Get the TextView object and set its content.



protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.select_rate);
Rate aRate=(Rate)getIntent().getSerializableExtra(Rate.class.getName());
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.select_rate_dlg_title);
TextView aTextView=(TextView)findViewById(R.id.selectRateDlgTitle);
aTextView.setText(aRate.getSupplier());
...
}