0% found this document useful (0 votes)
12 views

Mobile Application

Laying out controls in containers notes

Uploaded by

Gagan Mm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
12 views

Mobile Application

Laying out controls in containers notes

Uploaded by

Gagan Mm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 37
CHAPTER 3 Laying Out Controls in Containers A container is a view used to contain other views. Android offers a collection of view classes that act containers for views. These container classes are called and as the name suggests, they decide the organi- and position of their children views. layouts, zation, size) Let's start the chapter with an introduction to'different layouts used in Android applications. Introduction to Layouts Layouts are basically containers for other items known as views, which are displayed on the screen. Layouts help manage and arrange views as well. Layouts are defined in the form of XML files that cannot be changed by our code during runtime. Table 3.1 shows the layout managers provided by the Android SDK, a TABLE 3.1 Android Layout Managers Layout Manager _ Description LinearLayout ~ Organizes its children either horizontally or vertically Organizes its children relative to one another or to the parent Each child control is given a specific location within the bounds of the container RelativeLayout AbsoluteLayout IN THIS CHAPTER > Introduction to Layouts > LinearLayout > Applying the orientation Attribute > Applying Height and width Attributes > Applying the Padding Attribute > Applying the weight attribute > Applying the Gravity Attribute > Using the android: 1ayout_ aravity Attribute > RelativeLayout > Relative Layout Control Attributes > AbsoluteLayout > FrameLayout > TableLayout > TableLayout Operations > GridLayout > Screen Orientation Adaptations “£26299 CMRIT LIBRARY BANGALOR: - 26% 102 CHAPTER 3 Laying Out Controls in Containers Layout Manager _ Description FrameLayout Displays a single view: that is, the next view replaces the previous view and hence is used to dynamically change the children in the layout TableLayout Organizes its children in tabular form GridLayout Organizes its children in grid format ki ewGroups as one Or ‘The containers or layouts listed in Table 3.1 are also known as ViewSroups a5 i ‘a desired manner through them. Besides the more views are grouped and arranged in : : viewsroups shown here Android supports one more ViewGroup known as ScrollView, which is discussed in Chapter 4, “Utilizing Resources and Media. LinearLayout The LinearLayout is the most basic layout, horizontally or vertically. To arrange controls within butes are used: > android:orientation—Used for arranging the controls in the container in horizon- and it arranges its elements sequentially, either a linear layout, the following attri- tal or vertical order > android: layout_width—Used for defining the width of a control eight —Used for defining the height of a control > android: layout_! android:padding—Used for increasing the whitespace between the boundaries of the control and its actual content > android: layout_weight—Used for shrinking or expanding the size of the control to consume the extra space relative to the other controls in the container > android:gravity—Used for aligning content within a control > android: 1ayout_gravity—Used for aligning the control within the container Applying the orientation Attribute ‘The orientation attribute is used to arrange its children either in horizontal or verti- cal order. The valid values for this attribute are horizontal and vertical. If the value of the android:orientation attribute is set to vertical, the children in the linear layout are arranged in a column layout, one below the other. Similarly, if the value of the android:orientation attribute is set to horizontal, the controls in the linear layout are arranged in a row format, side by side. The orientation can be modified at runtime through the setorientation() method. That is, by supplying the values Horrzontan Or VERTICAL to the setorientation() method, we can arrange the children of the LinearLayout in row or column format, respectively. LinearLayout 103 Applying the height and width Attributes The default height and width of a control are decided on the basis of the text or content that is displayed through it. To spe: y a certain height and width to the Control, we use the android: Jayout_width and andyoid: layout height attributes. We can spe values for the height and wiaen attributes in the following three way > By supplying specific dimen: ap (device independent pixels fy the jon values for the control in terms of px (pixels), dip/ ), sp (scaled pixels), pts (points), in (inches), and mm (millimeters), For example, the android layout_width="20px" attribute sets the width of the control to 20 pixels, > By providing the value as wrap width, this attribute res content. When ass; igned to the control’s height or zes the control to expand to fit its contents. For example, when this value is applied to the width of the Textview, it expands so that its complete text is visible. By providing the value as match_parent. When assigned to the control's height or width, this attribute forces the size of the control to expand to fill up all the avail- able space of the enclosing container. For layout elements, the value wrap_content resizes the layout to fit the controls added as its children. The value match_paxent makes the layout expand to take up alll the space in the parent layout. Applying the padding Attribute The padding attribute is used to increase the whitespace between the boundaries of the control and its actual content. Through the android :padding attribute, we can set the same amount of padding or spacing on all four sides of the-control. Similarly, by using the android:paddingLeft, android: paddingRight, android:paddingTop, and android: paddingbot tom attributes, we can specify the individual spacing on the left, right, top, and bottom of the control, respectively. ‘The following example sets the spacing on all four sides of the control to § pix android: paddin Sdip" imilarly, the following example sets the spacing on the left side of the control to 5 pixels: ly 8 P F android:paddingLeft="Sdip" To set the padding at runtime, we can call the set Padaing() method. amoaaie 7 = 104 CHAPTER 3 Laying Out Controls in Containers Let’s see how the controls are laid out in the LinearLayout |: Create a new Android Project called LinearLayoutApp. The layout using an example, layout file activ: original default content of thy ity Linea_layout_app.xml appears as shown in Listing 3.1, USTING 3.1 Default Code in the La yout File activity linear_layout_app.xml ‘nttp://schemas andoid.com/apk/res/android" omins:tools="http: //schemas.android.com/tools" android: layout_widt! sRelativeLayout xmlns:andvoi inatch_parent* android: layout_height="match parent" » Let’s apply the LinearLayout and add three eutton controls to the layout. Modify the activity_linear_layout_app.xm) to appear as shown in Listing 3.2. LISTING ayout_app.xml File on Adding Three Button Controls out xmlns:androi "http: //schemas.android.com/apk/res/android* "match_parent" ‘cid: layout_height="natch_parent" android:orientatione"vertical” > Button android:id: +id/Mango" android:text="Mango" android: layout_widthe"match parent" android: layout height="wrap_content" /> eputton android: id="s+id/zanana" droid: tex! Banana" android: Jayout_widthe"match_parent" android: layout_height="«rap_content" /> Linearlayout 105 The orientation of LinearLayout is set to vertical, declaring that we want to arrange its child elements vertically, one below the other. h ‘The height and width of the layout are set to expand to fill up all the available space of the enclosing container, that is, the device screen. Three Button controls are added to the lay. . ‘out, which appear one below the other. The IDs and text assigned to the three Burton controls are Appie, Mango, and Banana, respectively. The height of the three controls is set to wrap_content, which is enough to accommodate the text. Finally, on i the width of the three controls is set to match_parent, so that the width of the three controls expands to fill up the available space of the LinearLayout container. We see the output shown in Figure 3.1. Eran @ LinearilayoutAppactivity FIGURE 3.1 Three Button controls arranged vertically in LinearLayout To see the controls appear horizontally, set the orientation attribute of the LinearLayout to horizontal. We also need to set the 1ayout_width attribute of the three controls to wrap_content; otherwise, we will be able to see only the first sutton control, the one with the appie ID. If the layout_width attribute of any control is set to match_parent, it takes up all the available space of the container, hiding the rest of the controls behind it. By setting the values of the 1ayout_width attributes to wrap_content, we make sure that the width of the control expands just to fit its content and does not take up all the available space. Let’s modify the activity_1inear_leyout_app.xmi to appear as shown in List- ing 3.3. LUSTING 3.3. The activity_linear_layout_app.xm1 File on Setting Horizontal Orientation to the sutton Controls

You might also like