Mindmajix

FrameLayout & LinearLayout in Android

FrameLayout

Frame Layout is designed to display a single item at a time. You can have multiple elements within a Frame Layout but each element will be positioned based on the top left of the screen. Elements that overlap will be displayed overlapping. Let us see how this works.

<FrameLayout

android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView

android:src="@drawable/icon"
android:scaleType="fitCenter"
android:layout_height="fill_parent" 
android:layout_width="fill_parent"/

<TextView

android:text="Learn-Android.com"
android:textSize="24sp"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>

</FrameLayout>

FrameLayout can become more useful when elements are hidden and displayed programmatically. You can use the attribute android:visibility in the XML to hide specific elements. You can call setVisibility from the code to accomplish the same thing. The three available visibility values are visible, invisible (does not display, but still takes up space in the layout), and gone (does not display, and does not take space in the layout).

LinearLayout

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute.

LinearLayout organizes elements along a single line. You specify whether that line is vertical or horizontal using android:orientation. Here is a sample Layout XML using LinearLayout.

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button

android:id="@+id/backbutton"
android:text="Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView

android:text="First Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText

android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView

android:text="Last Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<EditText

android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

Here is a screenshot of the result of the above XML.

Screenshot_11

 

 

 

 

 


 

0 Responses on FrameLayout & LinearLayout in Android"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.