You are on page 1of 9

Ch 8

Designing User Interfaces with

Layouts

Views and ViewGroups [1]


An Activity contains Views and ViewGroups. A View is a widget that has an appearance on screen for examples widgets are buttons, labels, text boxes, etc. A View derives from the base class android.view.View. One or more Views can be grouped together into a ViewGroup. A ViewGroup (which is by itself is a special type of View) provides the layout in which you can order the appearance and sequence of views, examples of Viewgroups are LinearLayout, FrameLayout, etc. A ViewGroup derives from the base class android.view.ViewGroup. A View that contains other View objects is called a parent view and the parent View contains View objects called child views, or children.

Views and ViewGroups [2]


Android supports the following ViewGroups: LinearLayout TableLayout RelativeLayout FrameLayout ScrollView

Exploring Various Layout Types


Layouts are derived from android.view.ViewGroup. The types of layouts built-in to the Android SDK framework include FrameLayout LinearLayout RelativeLayout TableLayout

LinearLayout
one of the simplest and most common types of layouts designed to display child View controls in a single row or column. as its name implies: it organizes controls linearly in either a vertical or horizontal fashion.

Defining a Linear Layout Programmatically


//Code Snippet TextView tv1 = new TextView(this); tv1.setText("FIRST"); tv1.setTextSize(100); tv1.setGravity(Gravity.CENTER); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); ll.setGravity(Gravity.CENTER); ll.addView(tv1); setContentView(ll);

FrameLayout
FrameLayout is designed to display a single item at a time or to display a stack

of child View items. used to show multiple controls within the same screen space, but each
View is drawn from the top-left corner of the layout Only reason use this Layout manager to display a single view but also populate it with many items setting one to visible and others to invisible.

Attributes

You might also like