AbsoluteLayout
Each child in an AbsoluteLayout is given a specific location within the bounds of the container. Such fixed locations make AbsoluteLayout incompatible with devices of different screen size and resolution. The controls in AbsoluteLayout are laid out by specifying their exact X and Y positions. The coordinate 0,0 is the origin and is located at the top-left corner of the screen.
Let’s write an application to see how controls are positioned in AbsoluteLayout. Create a new Android Project called AbsoluteLayoutApp. Modify its layout file, activity_ absolute_layout_app.xml, as shown in Listing 3.10.
Listing 3.10. The Layout File activity_absolute_layout_app.xml on Arranging Controls in the AbsoluteLayout Container
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Product Form" android:textSize="20sp" android.textStyle="bold" android:layout_x="90dip" android:layout_y="2dip"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Product Code:" android:layout_x="5dip" android:layout_y="40dip" /> <EditText android:id="@+id/product_code" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="100dip" android:layout_x="110dip" android:layout_y="30dip" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Product Name:" android:layout_x="5dip" android:layout_y="90dip"/> <EditText android:id="@+id/product_name" android:layout_width="200dip" android:layout_height="wrap_content" android:minWidth="200dip" android:layout_x="110dip" android:layout_y="80dip" android:scrollHorizontally="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Product Price:" android:layout_x="5dip" android:layout_y="140dip" /> <EditText android:id="@+id/product_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="100dip" android:layout_x="110dip" android:layout_y="130dip" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/click_btn" android:text="Add New Product" android:layout_x="80dip" android:layout_y="190dip" /> </AbsoluteLayout>
The controls in activity_absolute_layout_app.xml are as follows:
- The New Product Form TextView is set to appear 90dip from the left and 2dip from the top side of the container. The size of the text is set to 20sp, and its style is set to bold.
- The Product Code TextView is set to appear 5dip from the left and 40dip from the top side of the container.
- The product_code EditText control is set to appear 110dip from the left and 30dip from the top side of the container. The minimum width of the control is set to 100dp.
- The ProductName TextView control is set to appear 5dip from the left and 90dip from the top side of the container.
- The product_name EditText control is set to appear 110dip from the left and 80dip from the top side of the container. The minimum width of the control is set to 200dip, and its text is set to scroll horizontally when the user types beyond its width.
- The Product Price TextView is set to appear 5dip from the left and 140dip from the top side of the container.
- The product_price EditText control is set to appear 110dip from the left and 130dip from the top side of the container. The minimum width of the control is set to 100dip.
- The click_btn Button, Add New Product, is set to appear 80dip from the left and 190dip from the top side of the container.
If we don’t specify the x, y coordinates of a control in AbsoluteLayout, it is placed in the origin point, that is, at location 0,0. If the value of the x and y coordinates is too large, the control does not appear on the screen. The values of the x and y coordinates are specified in any units, such as sp, in, mm, and pt.
After specifying the locations of controls in the layout file activity_absolute_layout_app.xml, we can run the application. There is no need to make any changes in the file AbsoluteLayoutAppActivity.java. When the application is run, we get the output shown in Figure 3.11.
Figure 3.11. Different controls laid out in AbsoluteLayout
The AbsoluteLayout class is not used often, as it is not compatible with Android phones of different screen sizes and resolutions.
The next layout we are going to discuss is FrameLayout. Because we will learn to display images in FrameLayout, let’s first take a look at the ImageView control that is often used to display images in Android applications.