June 3, 2013  Cuelogic Insights

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.

Framelayout

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.

Related Reading

[wcp-carousel id=”10008″]

Read More

Recommended Content

Go Back to Main Page