We have already seen how it is possible to create a view or view group in XML and then to edit its properties from within Java code. It is in fact perfectly possible and quite easy to create any screen element from scratch at runtime. All of Android's built-in widgets are extensions of the View class and here we will use a DatePicker, create it, and then set some of its properties from within Java code.
As just mentioned, we will be doing very little in the way of XML in this recipe. Nevertheless, start a new Android project and navigate to the res/layout/main.xml
file.
Remove any default views that the project wizard may have created, so that all we have is an empty
LinearLayout
filling the screen:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout>
Give our layout...