Android - UI Layouts


What is View in android?

View is a object which is created from the View class and occupies a rectangular area on the screen and is responsible for drawing and event handling.

View is the base class for widgets, which are used to create interactive UI components like buttons, text fields, etc.


What is ViewGroup in android?

The ViewGroup is a subclass of View and provides invisible container that hold other Views or other ViewGroups and define their layout properties.

At third level we have different layouts which are subclasses of ViewGroup class and a typical layout defines the visual structure for an Android user interface.


How to define or create View in android?

It can be created either at run time using View/ViewGroup objects or you can declare your layout using simple XML file main_layout.xml which is located in the res/layout folder of your project.

XML file in res/layout folder to define View:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" >
 
 <TextView android:id="@+id/text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="This is a TextView" />
</LinearLayout>

Code in MainActivity file to create the view:

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

Layout Types in android?

Following are the layouts available in Android.

  • Linear Layout: LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally.
  • Relative Layout: RelativeLayout is a view group that displays child views in relative positions.
  • Table Layout: TableLayout is a view that groups views into rows and columns.
  • Absolute Layout: AbsoluteLayout enables you to specify the exact location of its children.
  • Frame Layout: The FrameLayout is a placeholder on screen that you can use to display a single view.
  • List View: ListView is a view group that displays a list of scrollable items.
  • Grid View: GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.

Layout Attributes in android?

Following are some important attributes available in Android Layout.

AttributeDescription
Landroid:idCommon to all, This is the ID which uniquely identifies the layout.
Linear Layout
android:baselineAlignedValues: true|false, prevents the layout from aligning its children's baselines.
android:dividerValues: any colors, This is drawable to use as a vertical divider between buttons.
android:orientationValues: vertical|horizontal, This specifies the direction of arrangmet.
Relative Layout
android:gravityValues: top|bottom|left|right|center|center_vertical|center_horizontal, This specifies how an object should position its content, on both the X and Y axes.
android:ignoreGravityValues: top|bottom|left|right|center|center_vertical|center_horizontal, This indicates what view should not be affected by gravity.
Table Layout
android:collapseColumnsThis specifies the zero-based index of the columns to collapse.
android:stretchColumnsThe zero-based index of the columns to stretch.
Absolute Layout
android:layout_xThis specifies the x-coordinate of the view.
android:layout_yThis specifies the y-coordinate of the view.
Frame Layout
android:foregroundValues: any colors,This defines the drawable to draw over the content.
android:foregroundGravityValues: top|bottom|left|right|center|center_vertical|center_horizontal,Defines the gravity to apply to the foreground drawable.
android:measureAllChildrenDetermines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false.
ListView Layout
android:dividerThis is drawable or color to draw between list items.
android:dividerHeightThis specifies height of the divider. This could be in px, dp, sp, in, or mm.
android:entriesSpecifies the reference to an array resource that will populate the ListView.
android:footerDividersEnabledValues: true|false, When set to false, the ListView will not draw the divider before each footer view.
android:headerDividersEnabledValues: true|false, When set to false, the ListView will not draw the divider after each header view.
Absolute Layout
android:columnWidthThis specifies the fixed width for each column. This could be in px, dp, sp, in, or mm.
android:horizontalSpacingDefines the default horizontal spacing between columns. This could be in px, dp, sp, in, or mm.
android:numColumnsValues:number|auto_fit, Defines how many columns to show. May be an integer value
android:stretchModeValues: none|spacingWidth|columnWidth|spacingWidthUniform, Defines how columns should stretch to fill the available empty space, if any.
android:verticalSpacingDefines the default vertical spacing between rows. This could be in px, dp, sp, in, or mm.

Next Article: Android UI Controls


For any questions, suggestions and feedback, Please write to us. Thanks for Reading :)

No comments:

Post a Comment