Search This Blog

Thursday 7 June 2012

Quick Layout Tutorial for Android - Frame layout

Frame Layout

Frame Layout display one control at a time. Frame layout is used to create the layers of components. All the components positions are calculated from the top left of the screen.

The components overlapping will display overlapped.

For Example :


<FrameLayout 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android">
 <ImageView 
  android:src="@drawable/logo"
  android:scaleType="fitCenter"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"/>
 <TextView
  android:text="CreativeAndroidApps"
  android:textSize="16sp"
  android:textColor="#000000"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:gravity="center"/>
</FrameLayout>

See the below screen shot for XML

Frame Layout






















To hide and show the components at run time the Frame Layout could be use by specify the visibility of the component.
In XML the android:visibility could be define to visible,invisible (take show but not visible),gone (not visible and not take any space).

setVisibility function will be used code fro same purpose.

See below how to use the Frame layouts in programming


 public void onCreate(Bundle savedInstanceState) {   
   super.onCreate(savedInstanceState);   
   ImageView iv = new ImageView(this);   
   iv.setImageResource(R.drawable.logo);   
   iv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));   
   iv.setScaleType(ScaleType.FIT_CENTER);   
   TextView tv = new TextView(this);
   tv.setText("CreativeAndroidApps");   
   tv.setTextSize(16);   
   tv.setGravity(Gravity.CENTER);
   tv.setTextColor(Color.BLACK);     
   FrameLayout fl = new FrameLayout(this);   
   fl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));   
   fl.addView(iv);   
   fl.addView(tv);   
   setContentView(fl);   
 }   

2 comments:

  1. Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Android Training in chennai | Android Training in chennai with placement

    ReplyDelete
  2. Very nice post,keep sharing more blogs with us.

    android developer training

    ReplyDelete