FrameLayout
FrameLayout is used to display a single View. The View added to a FrameLayout is placed at the top-left edge of the layout. Any other View added to the FrameLayout overlaps the previous View; that is, each View stacks on top of the previous one. Let’s create an application to see how controls can be laid out using FrameLayout.
In the application we are going to create, we will place two ImageView controls in the FrameLayout container. As expected, only one ImageView will be visible, as one ImageView will overlap the other ImageView, assuming both ImageView controls are of the same size. We will also display a button on the ImageView, which, when selected, displays the hidden ImageView underneath.
Let’s start with the application. Create a new Android project called FrameLayoutApp. To display images in Android applications, the image is first copied into the res/drawable folder and from there, it is referred to in the layout and other XML files. We look at the procedure for displaying images, as well as the concept of drawable resources, in detail in Chapter 4. For the time being, it is enough to know that to enable the image(s) to be referred to in the layout files placed in the res/drawable folder, the image needs to exist in the res/drawable folder. There are four types of drawable folders: drawable-xhdpi, drawable-hdpi, /res/drawable-mdpi, and /res/drawable-ldpi. We have to place images of different resolutions and sizes in these folders. The graphics with the resolutions 320 dpi, 240dpi, 160 dpi, and 120dpi (96 x 96 px, 72 x 72 px, 48 x 48 px, and 36 x 36 px), are stored in the res/drawable-xhdpi, res/drawable-hdpi, res/drawable-mdpi, and res/drawable-ldpi folders, respectively. The application picks up the appropriate graphic from the correct folder. So, if we copy two images called bintupic.png and bintupic2.png of the preceding size and resolution and paste them into the four res/drawable folders, the Package Explorer resembles Figure 3.12.
Figure 3.12. The Package Explorer window showing the two images, bintupic.png and bintupic2.png, dropped into the res/drawable folders
To display two ImageViews and a TextView in the application, let’s write the code in the layout file activity_frame_layout_app.xml as shown in Listing 3.11.
Listing 3.11. The Layout File activity_frame_layout_app.xml on Arranging the ImageView and TextView Controls in the FrameLayout Container
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/first_image" android:src = "@drawable/bintupic" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" /> <ImageView android:id="@+id/second_image" android:src = "@drawable/bintupic2" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click the image to switch" android:layout_gravity="center_horizontal|bottom" android:padding="5dip" android:textColor="#ffffff" android:textStyle="bold" android:background="#333333" android:layout_marginBottom="10dip" /> </FrameLayout>
The first_image and second_image ImageView controls are set to display the images bintupic.png and bintupic2.png, respectively. To make the two images stretch to cover the entire screen, the scaleType attribute in the ImageView tag is set to fitXY. A TextView, Click the image to switch, is set to display at the horizontally centered position and at a distance of 10dip from the bottom of the container. The spacing between the text and the boundary of the TextView control is set to 5dip. The background of the text is set to a dark color, the foreground color is set to white, and its style is set to bold. When a user selects the current image on the screen, the image should switch to show the hidden image. For this to occur, we need to write code in the activity file as shown in Listing 3.12.
Listing 3.12. Code Written in the Java Activity File FrameLayoutAppActivity.java
package com.androidunleashed.framelayoutapp; import android.app.Activity; import android.os.Bundle; import android.widget.ImageView; import android.view.View.OnClickListener; import android.view.View; public class FrameLayoutAppActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_frame_layout_app); final ImageView first_image = (ImageView)this.findViewById(R.id.first_image); final ImageView second_image = (ImageView)this.findViewById(R.id.second_image); first_image.setOnClickListener(new OnClickListener(){ public void onClick(View view) { second_image.setVisibility(View.VISIBLE); view.setVisibility(View.GONE); } }); second_image.setOnClickListener(new OnClickListener(){ public void onClick(View view) { first_image.setVisibility(View.VISIBLE); view.setVisibility(View.GONE); } }); } }
The two first_image and second_image ImageView controls are located through the findViewById method of the Activity class and assigned to the two ImageView objects, first_image and second_image, respectively. We register the click event by calling the setOnClickListener() method with an OnClickListener. An anonymous listener is created on the fly to handle click events for the ImageView. When the ImageView is clicked, the onClick() method of the listener is called. In the onClick() method, we switch the images; that is, we make the current ImageView invisible and the hidden ImageView visible. When the application runs, we see the output shown in Figure 3.13 (left). The application shows an image, and the other image is hidden behind it because in FrameLayout one View overlaps the other. When the user clicks the image, the images are switched, as shown in Figure 3.13 (right).
Figure 3.13. (left) An image and a TextView laid out in FrameLayout, and (right) the images switch when clicked