Android FrameLayout




Android FrameLayout


1. Static FrameLayout Creation in Android
2. Dynamic FrameLayout Creation in Android
3. Run time FrameLayout Creation in Android


1. Static FrameLayout Creation in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="21dp"
        android:textSize="25dp"
        android:text="Jelly Bean" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="5dp" >
   
         <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/android" />
     
         <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="85dp"
        android:layout_gravity="center_horizontal"
        android:textSize="25dp"
        android:textColor="#0000FF"
        android:text="Static" />
   
    </FrameLayout>

</RelativeLayout>

Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

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

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Step 4 : Open AndroidManifest.xml and add following code :


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5 : Our output will be like this :



2. Dynamic FrameLayout Creation in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</RelativeLayout>


Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

RelativeLayout rl;
FrameLayout fl;
TextView t1;
ImageView img;
RelativeLayout.LayoutParams param1, param2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        rl = (RelativeLayout) findViewById(R.id.rl);
        fl = new FrameLayout(MainActivity.this);
   
        param1 = new RelativeLayout.LayoutParams
         ((int) RelativeLayout.LayoutParams.WRAP_CONTENT,
         (int) RelativeLayout.LayoutParams.WRAP_CONTENT);
   
        param2 = new RelativeLayout.LayoutParams
         ((int) RelativeLayout.LayoutParams.WRAP_CONTENT,
         (int) RelativeLayout.LayoutParams.WRAP_CONTENT);
   
        t1 = new TextView(MainActivity.this);
        img = new ImageView(MainActivity.this);
   
        t1.setText("Hemanth Somaraju");
        t1.setTextSize(25);
        param1.setMargins(150, 50, 0, 0);
        t1.setLayoutParams(param1);
   
        param2.setMargins(0, 50, 0, 0);
        fl.setLayoutParams(param2);
   
        img.setImageResource(R.drawable.android);
        img.setLayoutParams(new LayoutParams
         (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        fl.addView(img);
           
        rl.addView(t1);
        rl.addView(fl);    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Step 4 : Open AndroidManifest.xml and add following code :


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5 : Our output will be like this :



3. Run time FrameLayout Creation in Android
Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:textSize="20dp"
        android:textColor="#FF0000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="FrameLayout" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="Create" />

</RelativeLayout>


Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

RelativeLayout rl;
Button b;
FrameLayout frame1, frame2;
TextView t1;
RelativeLayout.LayoutParams param1, param2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        rl = (RelativeLayout) findViewById(R.id.rl);
   
        b = (Button) findViewById(R.id.button1);
   
        b.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

rl.removeViewInLayout(frame2);

frame1 = new FrameLayout (MainActivity.this);

param1 = new RelativeLayout.LayoutParams
((int) RelativeLayout.LayoutParams.WRAP_CONTENT,
         (int) RelativeLayout.LayoutParams.WRAP_CONTENT);

param2 = new RelativeLayout.LayoutParams
((int) RelativeLayout.LayoutParams.WRAP_CONTENT,
         (int) RelativeLayout.LayoutParams.WRAP_CONTENT);

t1 = new TextView(MainActivity.this);

t1.setText("Hemanth 1");
t1.setTextSize(25);
param1.setMargins(150, 50, 0, 0);
t1.setLayoutParams(param1);

param2.setMargins(100, 250, 0, 0);
frame1.setLayoutParams(param2);

frame1.addView(t1);

rl.addView(frame1);
}
});
           
        b.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub

rl.removeViewInLayout(frame1);

frame2 = new FrameLayout (MainActivity.this);

param1 = new RelativeLayout.LayoutParams
((int) RelativeLayout.LayoutParams.WRAP_CONTENT,
         (int) RelativeLayout.LayoutParams.WRAP_CONTENT);

param2 = new RelativeLayout.LayoutParams
((int) RelativeLayout.LayoutParams.WRAP_CONTENT,
         (int) RelativeLayout.LayoutParams.WRAP_CONTENT);

t1 = new TextView(MainActivity.this);

t1.setText("Somaraju 2");
t1.setTextSize(25);
param1.setMargins(150, 50, 0, 0);
t1.setLayoutParams(param1);

param2.setMargins(100, 250, 0, 0);
frame2.setLayoutParams(param2);

frame2.addView(t1);

rl.addView(frame2);

return true;
}
});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Step 4 : Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5 : Our output will be like this :