Recipe 2.12 Using FrameLayout for View Positioning

Android Versions
Level 1 and above
Permissions
None
Source Code to Download from Wrox.com
UsingFrameLayout.zip

The FrameLayout is a placeholder on screen that you can use to display a single view. Views that you add to a FrameLayout are always anchored to the top left of the layout. This recipe shows how to use the FrameLayout to create a simple image viewer.

Solution

For this recipe, assume you have three images located in the res/drawable-mdpi folder (see Figure 2-22).

You also need to add the following code snippet in the activity_main.xml file:

<RelativeLayout android:id="@+id/RLayout" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:id="@+id/lblComments" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, Android!" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" /> <FrameLayout android:id="@+id/framelayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/lblComments" android:layout_below="@+id/lblComments" android:layout_centerHorizontal="true" > <ImageView android:src = "@drawable/galaxy_s3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:tag="1" ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.