Close
Topic : User Interface - Frame Layout: Example



Frame Layout:

Frame Layout is one of the most efficient and simplest layouts used by Android to organize view controls. We use Frame layout as a container to displays only one view, or views which overlap.



Frame Layout Understanding: infobrother

The Frame layout is often used as a container to displays only one view. but we can overlap other views, and control their position within the frame layout by assigning gravity to each child.

Following are the commonly used properties specific to Frame Layout provided by Android.


1) Foreground:

Foreground Defines the Drawable to draw over the content. It could be color value. the Possible color values can be in the form of:


  • » rgb: Red - Green - Blue. (For Example - #184757)
    » argb: Alpha - Red - Green - Blue. Alpha "a" is the transparency values in the range 0 - 255. O corresponds to full transparency and 255 corresponds to full opacity. (for Example - #ff184757)


android:foreground="#008080"

By applying foreground feature, all the views taken in the frame layout will go to the background and the frame layout comes in the foreground.



2) ForegroundGravity:

Using foregroundGravity, we can set the Gravity to the foreground views. The default value is Fill. We can set the gravity to views in the form of "Top, bottom, left, right, center, center_vertical, center_horizontal, clip_vertical, clip_horizontal"


android:foregroundGravity="center_horizontal"

Using above Syntax, we can set the gravity of foreground center horizontally. we can also set multiple values by using "|" vertical bar.


android:foregroundGravity="top | left"


3) Visibility:

We can make the view Visible, Invisible or gone using the Visibility property:


  • » Visible: The View is Present and visible.
    » Invisible: The View is present but invisible.
    » Gone: The view is Neither present nor visible.


android:visibility="invisible"


4) MeasureAllChildren:

Determines whether to measure all children Including Gone state, or just those in the VISIBLE or INVISIBLE state when measuring. The default value is false and we can set values in the Boolean form, i.e. "true" or "false".


android:measureAllChildren="true"

If MeasureAllChildren is set true, then it will show actual width and height of frame Layout even if the views visibility is in gone state.



Declaration:

Here is the basic syntax to declare Frame Layout.


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <!--Add Children here-->
</FrameLayout>



Look at the given example to Know, How we can design the frame using frame layout in android.



Frame Layout Example: infobrother
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
  
  <!--include your own background Image in drawable folder.-->
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/background" />
  
  <!--Include Logo or another image. gravity = top-->
    <ImageView
        android:src="@drawable/logo"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:id="@+id/imageView2"
        android:layout_gravity="top" />
  
  <!--text view. gravity = center.-->
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:background="#AA000000"
        android:padding="12dp"
        android:text="@string/website"
        android:textColor="#FFFFFF"
        android:textSize="22sp"
        android:gravity="center"
        android:textStyle="bold" />
  
  <!--textView, gravity = bottom right-->
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|bottom"
        android:layout_marginLeft="5dp"
        android:background="#AA008080"
        android:padding="10dp"
        android:text="www.infobrother.com"
        android:textColor="#FFFFFF"
        android:textSize="18sp" />
  
  <!--textview, gravity = center-->
    <TextView
        android:text="@string/text"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:background="#AA000000"
        android:padding="10dp"
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="normal"
        android:layout_marginTop="30dp" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">Frame Layout</string>

  <string name="text"> INFOBROTHER IS FREE \n
  ONLINE WELFARE PROJECT: \n 
  INFOBROTHER is an Informative website,\n         
  where we focus on Programming, and \n          
  Try to Motivate people to \n 
  join Programming world. "</string>

  <string name="website">InfoBrother - The Land Of Learning</string>
</resources>
using Android.App;
using Android.OS;

namespace XLayout
{
    [Activity(Label = "Frame Layout", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
        }
    }
}


















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement