Android FrameLayout Example

The Frame Layout is designed to display single item at a time , you can add multiple views inside in it,In framelayout the childviews are arranged in the form of stack ,with the most recently added view will be at the top

Lets see an example of framelayout that cantains two textview with different background color

Project Detail
Project Name FrameLayoutDemo
Package com.pavan.framelayoutdemo
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout


create a xml with framelayout view containing two textview in it with different background color .

file :-frame_layout.xml 
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mainlayout"
 android:layout_height="fill_parent"
 android:layout_width="fill_parent"
 android:orientation="vertical" />

<!-- Green Background -->
        <Textview
               android:background="#00FF00"
               android:gravity="bottom"
               android:id="@+id/textView1"
               android:layout_height="200dp"
               android:layout_width="200dp"
               android:text="TextView One"
               android:textsize="15dp" />

<!-- BLUE Background -->
       <Textview
         android:background="#0000FF"
         android:gravity="bottom"
         android:id="@+id/textView2"
         android:layout_height="100dp"
         android:layout_width="100dp"
         android:text="TextView Two"
         android:textsize="15dp" />

</Framelayout>


Inside the framelayout firstly we added textview with green background and next we added another textview with blue background so the most recently added textview that the blue background one will be displayed at top.


2. Activity


Create a class FrameLayout and extend it to activity class and override the onCreate() method of activity class and set the content of the actvity with the above defined xml file by calling setContentView() method and passing xml file name as a parameter to it
                          setContentView(frame_layout);

file :- FrameLayout.java
package com.pavan.framelayoutdemo;

import android.app.Activity;
import android.os.Bundle;

public class FrameLayout extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.frame_layout);
 }

}

3. RUN


Android FrameLayout



1 comment:

Anu Chaudhary said...

I've developed a demo app as per above code.but it was giving an error:requires APK 14(min is 8).I settled it doown by changing min sdkversion in android manifest.xml.Error got resolved.but it is not working on my emulator.Unfortunately,gridlay(my activity name) stopped working.

Post a Comment