FrameLayout in Android

Frame layout in android provides a way to place the child views in a stack. The view that’s added last is on the top. The frame layout sizes itself according to the size of its largest child(with padding). You can control a child’s position within the frame layout by assigning gavity to the child using the android:layout_gravity attribute.

Let’s see how to use the FrameLayout in android.

Here, I will create a simple app that contains a FrameLayout. The FrameLayout contains two ImageView and a TextView.

The first image is for the background and the second image is for the logo. Now let’s see the code.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="405dp"
        android:layout_height="261dp"
        android:layout_gravity="center"
        app:srcCompat="@drawable/island_blur" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="128dp"
        android:layout_height="191dp"
        android:layout_gravity="center|left"
        app:srcCompat="@drawable/logo" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="255dp"
        android:layout_height="133dp"
        android:layout_gravity="center|right"
        android:gravity="center|left"
        android:text="DeveloperXon"
        android:textColor="#FFFFFF"
        android:textSize="36sp" />
</FrameLayout>

Here, The first ImageView will be at the bottom and the TextView that I added last will be at the top.

MainActivity.java

package com.example.frame_layout_example;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

Output :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: