Using FrameLayout for designing XML Layouts in Android.

Using FrameLayout for designing XML Layouts in Android

In Android app development, there are many Layout Managers which help you arrange(layout) UI elements on the screen. This eases the development process of apps while keeping the layout and logic completely separate. For e.g. ,

  • LinearLayout arranges elements side by side either horizontally or vertically.
  • RelativeLayout helps you arrange your UI elements based on specific rules. You can specify rules like: align this to parent’s left edge, place this to the left/right of this elements etc.
  • AbsoluteLayout is for absolute positioning i.e. you can specify exact co-ordinates where the view should go.
  • TableLayout, we create a table with rows and columns and place elements within them. In each row, you can specify one or more elements.The table row is created using the tag inside the TableLayout.
  • FrameLayout allows placements of views along Z-axis. That means that you can stack your view elements one above the other. These are used less often than some other layouts, simply because they are generally used to display only one view, or views which overlap. The size of the FrameLayout is the size of its largest child (plus any padding).

Let us consider that, we have to create the following view/design for our app.

As you can see in the image, there are multiple views here, which are on top of each other with their respective layout positions (i.e. for e.g. the image saying “What if your peers decide your bonus” is on top of the background image and is positioned at bottom). To achieve this design we can
create an XML layout file and use the below code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@+id/imageviewlayout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:paddingBottom="2dp"
        android:paddingLeft="2dp"
        android:paddingRight="0dp"
        android:paddingTop="2dp" >

        <ImageView
            android:id="@+id/linearlayoutforFeatureImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="1dp"
            android:adjustViewBounds="true"
            android:background="@drawable/yourMainImage"
            android:clickable="true"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/sectionText"
            android:layout_width="wrap_content"
            android:layout_height="24dip"
            android:background="@drawable/yourBackGroundImage"
            android:gravity="center_vertical"
            android:paddingLeft="10dip"
            android:paddingRight="10dp"
            android:text="Action Resource Series"
            android:textColor="#bbbbbb"
            android:textSize="12sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/homescreennew_textProductDetailFeatureID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           android:layout_gravity="bottom"
            android:layout_marginBottom="-1dp"
            android:layout_marginTop="10dip"
            android:background="@drawable/yourBackGroundImage"
            android:gravity="center_vertical"
            android:maxLines="2"
            android:paddingLeft="10dip"
            android:paddingRight="10dp"
            android:text="What If Your Peers Decide Your Bonus"
            android:textColor="#ffffff"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/arrowimageFeatureID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_marginBottom="32dip"
            android:layout_marginRight="5dip"
            android:gravity="center_vertical"
            android:src="@drawable/list_arrow" />
    </FrameLayout>

</RelativeLayout>

Here, the frame layout is used as a container, with all the other child views inside it. Child views are drawn in a stack, with the most recently added child on top. You can, however, control their position within the FrameLayout by assigning gravity to each child, using the
android:layout_gravity attribute.

The android:layout_gravity attribute used here will help you understand how to position the child views according to the requirement shown above.

You can also define a FrameLayout programmatically, as follows,

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        TextView tv1 = new TextView(this);
        tv1.setText("Action Resource Series");
        tv1.setTextSize(YourSize);
        tv1.setTextColor(Color.BLACK);

        TextView tv2 = new TextView(this);
        tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT, Gravity.BOTTOM));
        tv2.setTextSize(50);
        tv2.setGravity(Gravity.BOTTOM);
        tv2.setText("What If Your Peers Decide Your Bonus");
        tv2.setTextColor(Color.WHITE);

        ImageView iv1 = new ImageView(this);
        iv1.setImageResource(R.drawable.yourImage);
        iv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT));
        iv1.setScaleType(ScaleType.MATRIX);

        ImageView iv2 = new ImageView(this);
        iv2.setImageResource(R.drawable.yourImage);
        iv2.setGravity(Gravity.BOTTOM);
        iv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT));
        iv1.setScaleType(ScaleType.MATRIX);

        FrameLayout fl = new FrameLayout(this);
        fl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.FILL_PARENT));
        fl.addView(iv1);
        fl.addView(iv1);
        fl.addView(tv1);
        fl.addView(tv2);
        fl.addView(iv2);
        setContentView(fl);

    }

Thus, the FrameLayout allows developers to display either a single or multiple UI elements within FrameLayout. Each element will be positioned based on the top left of the screen, and elements that overlap each other will be displayed overlapping.