Examples of how to use the android.view and android.widget platform APIs. For information about view and widget objects, see the topic "Designing the UI for an Android application" in the SDK documentation.
android:layout_height="wrap-content"
,
as is the height of each child. Each text view is as tall as it needs
to be, and the height of the LinearLayout itself is the sum of the height of its children.
//device/samples/SampleCode/src/com/android/sdk/view/LinearLayout1.java | Loads the linear_layout_1 layout resource |
//device/samples/SampleCode/assets/res/any/layout/linear_layout_1.xml | Defines the layout |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
android:layout_height="fill-parent"
,
so the LinearLayout fills the screen. Each text view is as tall as it needs
to be, so the LinearLayout just stacks them from top to bottom.
//device/samples/SampleCode/src/com/android/sdk/view/LinearLayout2.java | Loads the linear_layout_2 layout resource |
//device/samples/SampleCode/assets/res/any/layout/linear_layout_2.xml | Defines the layout |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
android:layout_weight="1"
. This means that it
will get all of the extra space left over after the LinearLayout has sized all of its children.
//device/samples/SampleCode/src/com/android/sdk/view/LinearLayout3.java | Loads the linear_layout_3 layout resource |
//device/samples/SampleCode/assets/res/any/layout/linear_layout_3.xml | Defines the layout |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
android:layout_weight="1"
and android:layout_width="0"
.
This means that each child is initially given a width of 0, and then all of the
remaining space (the width of the screen) is divided equally among the four
views.
//device/samples/SampleCode/src/com/android/sdk/view/LinearLayout4.java | Loads the linear_layout_4 layout resource |
//device/samples/SampleCode/assets/res/any/layout/linear_layout_4.xml | Defines the layout |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
layout_gravity
attribute to position the horizontal layout
on the right side of the screen
//device/samples/SampleCode/src/com/android/sdk/view/LinearLayout5.java | Loads the linear_layout_5 layout resource |
//device/samples/SampleCode/assets/res/any/layout/linear_layout_5.xml | Defines the layout |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
android:layout_alignParentTop="true"
.
The second view (view2) is pinned to the bottom of the screen: android:layout_alignParentBottom="true"
.
This demonstrates how views can be positioned relative to the RelativeLayout itself.
Views can also be positioned relative to each other as well. In this example, view3 is below view1 and above
view2: android:layout_above="view2" android:layout_below="view1"
. This has the effect of making
view3 stretch between view1 and view2.
Note that since view3 depends on the positions of both view1 and view2, it is defined after them in the layout file.
//device/samples/SampleCode/src/com/android/sdk/view/RelativeLayout1.java | Loads the relative_layout_1 layout resource |
//device/samples/SampleCode/assets/res/any/layout/relative_layout_1.xml | Defines the layout |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
android:layout_below
and
android:layout_below
and android:layout_toLeft attributes.
- Aligning the top edges of the buttons with the
android:layout_alignTop attribute
- Right-aligning the OK button with
android:layout_alignParentRight
- Using a margin to put some space between buttons
//device/samples/SampleCode/src/com/android/sdk/view/RelativeLayout2.java | Loads the relative_layout_2 layout resource |
//device/samples/SampleCode/assets/res/any/layout/relative_layout_2.xml | Defines the layout |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
The ScrollView is used to implement vertical scrolling. It does not display any content of its own. Instead, it assumes it has one child and pans up and down to keep the interesting area of its child in view.
In this example, a ScrollView is used to wrap a LinearLayout. The LinearLayout
in turn contains a stack of TextViews and Buttons. The ScrollView is as wide as
the screen (android:layout_width="fill-parent"
) and tall enough to
wrap the LinearLayout (android:layout_height="wrap-content"
). The
LinearLayout uses the same parameters, so it is also as wide as the screen and is as
tall as the sum of the heigts of all of its children.
//device/samples/SampleCode/src/com/android/sdk/view/ScrollView1.java | Loads the scroll_view_1 layout resource |
//device/samples/SampleCode/assets/res/any/layout/scroll_view_1.xml | Defines the layout |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
ListViews are highly customizable: you can change where the underlying data comes from, the internal representation of the data, and the Views that are used to display the data on the screen. All of this is done with a ListAdapter class. The Android platform includes some ListAdapters that are ready to use, or you can make your own to display custom information. (See ListView Example 4 and ListView Example 5.)
This example uses an existing ListAdapter called ArrayListAdapter. This adapter uses generics to map an array of objects to TextViews. In this case we are using an array of Strings.
Note that this example does not have a layout file. This is because the List1 class derives from ListScreen, which will provide a default layout if your activity does not provide an override.
//device/samples/SampleCode/src/com/android/sdk/view/List1.java | Contains code for the List1 class |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
In this example, the SimpleCursorListAdapter is provided with a cursor that contains a list of all people. Each row will be displayed using the Views defined in this XL file: //device/apps/common/assets/res/any/layout/simple_list_item_1.xml.
When creating a SimpleCursorListAdapter, you also provide a mapping from column names in the Cursor to view ids in the template file. In this case we are mapping the People.NAME column to the "text1" TextView.
//device/samples/SampleCode/src/com/android/sdk/view/List2.java | Contains code for the List2 class |
//device/apps/common/assets/res/any/layout/simple_list_item_1.xml | Defines the XML template used for each row. (Note that this file is provided as part of the Android platform.) |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
This example still uses a SimpleCursorListAdapter, but changes the following:
{ Phones.NAME, Phones.NUMBER }
to the two views
{ "text1", "text2"}
//device/samples/SampleCode/src/com/android/sdk/view/List3.java | Contains code for the List3 class |
//device/apps/common/assets/res/any/layout/simple_list_item_2.xml | Defines the XML template used for each row. (Note that this file is provided as part of the Android platform.) |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
This example introduces a SongListAdapter, which gets its data from an array of titles and an array of lyrics. The SongListAdapter then produces a SongView which is capable of displaying this data.
This example also shows how the ListView handles scrolling large items.
//device/samples/SampleCode/src/com/android/sdk/view/List4.java | Contains code for the List4 class, along with a custom ListAdapter and custom View to display the data. |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
This example needs some cleanup.
//device/samples/SampleCode/src/com/android/sdk/view/List5.java | Contains code for the List5 class, along with a custom ListAdapter. |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |
Note: This example does not support multi-line or right-to-left text. It is sample code only and should not be used in place of the TextView.
//device/samples/SampleCode/src/com/android/sdk/view/CustomView1.java | Loads the custom_view_3 layout resource |
//device/samples/SampleCode/src/com/android/sdk/view/LabelView.java | Implementation of the custom view |
//device/samples/SampleCode/assets/res/any/layout/custom_view_1.xml | Defines a layout that uses LabelViews |
//device/samples/SampleCode/AndroidManifest.xml | Defines the activity |