0% found this document useful (0 votes)
15 views25 pages

Android Studio Linear Layout Guide

Mobile Application Development notes

Uploaded by

Prabhas J
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views25 pages

Android Studio Linear Layout Guide

Mobile Application Development notes

Uploaded by

Prabhas J
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Mobile Application Development: Notes on Layouts in Android Studio

Linear Layout in Android Studio


Definition of Linear Layout
A LinearLayout in Android Studio is a ViewGroup that arranges its child views in a single
direction, either vertically or horizontally. It’s a simple and efficient layout that stacks child
elements one after another, ensuring that each element is displayed in a linear sequence based on
the order of their addition.

Applications of Linear Layout


LinearLayout is best used in scenarios where a straightforward, ordered arrangement of UI
elements is required. Typical use cases include:

 Form Layouts: Aligning form fields, labels, and buttons in a vertical sequence.
 Toolbars and Navigation Menus: Horizontally aligning buttons or icons.
 Basic Lists: Displaying a series of items in a single column or row.

Important Attributes of Linear Layout


Key attributes of LinearLayout, which help to control the behavior and appearance of the layout
and its child views:

1. android:orientation
o Description: Specifies the direction (horizontal or vertical) in which the child
views are aligned.
o Syntax: android:orientation="horizontal|vertical"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Child Views -->
</LinearLayout>

2. android:gravity
o Description: Controls the alignment of all child views within the LinearLayout
(e.g., center, left, right).
o Syntax: android:gravity="center"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

android:gravity="center">
<!-- Child Views -->
</LinearLayout>

3. android:layout_gravity
o Description: Specifies the alignment of a single child view within its parent
layout.
o Syntax: android:layout_gravity="center"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Centered Button"/>
</LinearLayout>

4. android:layout_weight
o Description: Defines how much space a view should occupy relative to others.
o Syntax: android:layout_weight="float_value"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button 2"/>
</LinearLayout>

5. android:weightSum
o Description: Specifies the maximum sum of layout weights for all child views
within a LinearLayout.
o Syntax: android:weightSum="float_value"
o Example:

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="horizontal">
<!-- Child Views -->
</LinearLayout>

6. android:baselineAligned
o Description: Indicates whether the layout should align its children based on their
baselines (for text views).
o Syntax: android:baselineAligned="true|false"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false">
<!-- Child Views -->
</LinearLayout>

7. android:baselineAlignedChildIndex
o Description: Specifies the index of the child that should be baseline-aligned.
o Syntax: android:baselineAlignedChildIndex="index"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAlignedChildIndex="1">
<!-- Child Views -->
</LinearLayout>

8. android:divider
o Description: Sets a drawable as a divider between child views.
o Syntax: android:divider="@drawable/divider"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/divider"
android:orientation="vertical">
<!-- Child Views -->
</LinearLayout>

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

9. android:showDividers
o Description: Controls where dividers are shown (none, beginning, middle, end).
o Syntax: android:showDividers="none|beginning|middle|end"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/divider"
android:showDividers="middle|end"
android:orientation="vertical">
<!-- Child Views -->
</LinearLayout>

10. android:dividerPadding
o Description: Specifies the padding around dividers.
o Syntax: android:dividerPadding="dimension"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/divider"
android:dividerPadding="8dp"
android:orientation="vertical">
<!-- Child Views -->
</LinearLayout>

11. android:padding
o Description: Sets padding around all sides of the LinearLayout.
o Syntax: android:padding="dimension"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<!-- Child Views -->
</LinearLayout>

12. android:layout_margin
o Description: Specifies the margin outside of the LinearLayout around all sides.
o Syntax: android:layout_margin="dimension"
o Example:

<LinearLayout

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<!-- Child Views -->
</LinearLayout>

13. android:background
o Description: Sets the background color or drawable for the LinearLayout.
o Syntax: android:background="@color/colorPrimary"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<!-- Child Views -->
</LinearLayout>

14. android:minWidth
o Description: Sets the minimum width of the LinearLayout.
o Syntax: android:minWidth="dimension"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="100dp">
<!-- Child Views -->
</LinearLayout>

15. android:minHeight
o Description: Sets the minimum height of the LinearLayout.
o Syntax: android:minHeight="dimension"
o Example:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp">
<!-- Child Views -->
</LinearLayout>

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

Methods of Linear Layout


Essential methods for working with LinearLayout:

1. setOrientation(int orientation)
o Description: Sets the orientation of the LinearLayout to either horizontal or
vertical.
o Syntax: [Link]([Link]);
o Example:

LinearLayout linearLayout = findViewById([Link]);


[Link]([Link]);

2. getOrientation()
o Description: Returns the current orientation of the LinearLayout.
o Syntax: int orientation = [Link]();
o Example:

int currentOrientation = [Link]();

3. setGravity(int gravity)
o Description: Sets the gravity for the LinearLayout, affecting the alignment of its
child views.
o Syntax: [Link]([Link]);
o Example:

LinearLayout linearLayout = findViewById([Link]);

3. getGravity()
o Description: Retrieves the current gravity setting of the LinearLayout.
o Syntax: int gravity = [Link]();
o Example:

int currentGravity = [Link]();

4. setWeightSum(float weightSum)
o Description: Sets the maximum sum of layout weights for all child views in the
LinearLayout.
o Syntax: [Link](3.0f);
o Example:

LinearLayout linearLayout = findViewById([Link]);


[Link](3.0f);

5. getWeightSum()
o Description: Retrieves the current weight sum of the LinearLayout.

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

o Syntax: float weightSum = [Link]();


o Example:

float currentWeightSum = [Link]();

6. setDividerDrawable(Drawable divider)
o Description: Sets a drawable as the divider between child views.
o Syntax:
[Link](getResources().getDrawable([Link]
r));
o Example:

LinearLayout linearLayout = findViewById([Link]);


[Link](getResources().getDrawable([Link]
r));

7. setShowDividers(int showDividers)
o Description: Controls where dividers are shown: beginning, middle, end, or a
combination.
o Syntax:
[Link](LinearLayout.SHOW_DIVIDER_MIDDLE);
o Example:

LinearLayout linearLayout = findViewById([Link]);


[Link](LinearLayout.SHOW_DIVIDER_MIDDLE);

8. setDividerPadding(int padding)
o Description: Sets padding around the dividers.
o Syntax: [Link](8);
o Example:

LinearLayout linearLayout = findViewById([Link]);


[Link](8);

9. setBaselineAligned(boolean baselineAligned)
o Description: Sets whether the LinearLayout aligns its children by their baselines.
o Syntax: [Link](false);
o Example:

LinearLayout linearLayout = findViewById([Link]);


[Link](false);

10. isBaselineAligned()
o Description: Checks whether the LinearLayout aligns its children by their
baselines.
o Syntax: boolean isBaselineAligned = [Link]();

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

o Example:

boolean baselineAligned = [Link]();

Relative Layout in Android Studio


Definition of Relative Layout
RelativeLayout is a type of layout in Android that allows you to position and size child views
relative to each other or to the parent container. Unlike LinearLayout, where children are stacked
vertically or horizontally, RelativeLayout lets you define how each child view should be
positioned relative to other views or the parent layout. This provides more flexibility and allows
for complex layouts to be constructed more efficiently.

Relative Layout Object Inheritance


RelativeLayout inherits from ViewGroup, which is a base class for all layout objects in
Android. ViewGroup itself inherits from View, which is the base class for all UI components in
Android.

[Link]
↳ [Link]

Applications for Relative Layout


RelativeLayout is ideal for applications where you need to position views in a flexible way
relative to each other or the parent container. This is particularly useful in scenarios such as:

 Complex user interfaces where elements are dynamically arranged.


 Forms where fields are positioned relative to each other.
 Layouts with overlapping views or components that need to adjust based on screen size
or orientation.

Important Properties/Attributes of RelativeLayout


1. layout_width
o Description: Defines the width of the RelativeLayout.
o Syntax: android:layout_width="match_parent" or
android:layout_width="wrap_content"
o Example:

<RelativeLayout

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

android:layout_width="match_parent"
android:layout_height="wrap_content">

2. layout_height
o Description: Defines the height of the RelativeLayout.
o Syntax: android:layout_height="match_parent" or
android:layout_height="wrap_content"
o Example:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

3. layout_alignParentTop
o Description: Aligns the view with the top of the parent.
o Syntax: android:layout_alignParentTop="true"
o Example:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>

4. layout_alignParentBottom
o Description: Aligns the view with the bottom of the parent.
o Syntax: android:layout_alignParentBottom="true"
o Example:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>

5. layout_alignParentLeft
o Description: Aligns the view with the left edge of the parent.
o Syntax: android:layout_alignParentLeft="true"
o Example:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>

6. layout_alignParentRight
o Description: Aligns the view with the right edge of the parent.
o Syntax: android:layout_alignParentRight="true"
o Example:

<Button
android:layout_width="wrap_content"

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>

7. layout_centerHorizontal
o Description: Centers the view horizontally within its parent.
o Syntax: android:layout_centerHorizontal="true"
o Example:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>

8. layout_centerVertical
o Description: Centers the view vertically within its parent.
o Syntax: android:layout_centerVertical="true"
o Example:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"/>

9. layout_toLeftOf
o Description: Positions the view to the left of the specified view.
o Syntax: android:layout_toLeftOf="@+id/view_id"
o Example:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/button1"/>

10. layout_toRightOf
o Description: Positions the view to the right of the specified view.
o Syntax: android:layout_toRightOf="@+id/view_id"
o Example:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

android:layout_toRightOf="@id/button1"/>

11. layout_above
o Description: Positions the view above the specified view.
o Syntax: android:layout_above="@+id/view_id"
o Example:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button1"/>

12. layout_below
o Description: Positions the view below the specified view.
o Syntax: android:layout_below="@+id/view_id"
o Example:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"/>

13. layout_alignBaseline
o Description: Aligns the baseline of the view with the baseline of the specified
view.
o Syntax: android:layout_alignBaseline="@+id/view_id"
o Example:

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/textView1"/>

14. layout_margin
o Description: Sets the margin space around the view.
o Syntax: android:layout_margin="16dp"

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

o Example:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"/>

15. layout_padding
o Description: Sets the padding space inside the view.
o Syntax: android:layout_padding="16dp"
o Example:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_padding="16dp"/>

Important Methods of RelativeLayout

1. addView(View child)
o Description: Adds a child view to the RelativeLayout.
o Syntax: public void addView(View child)
o Example:

RelativeLayout layout = findViewById([Link]);


Button button = new Button(this);
[Link](button);

2. removeView(View view)
o Description: Removes a child view from the RelativeLayout.
o Syntax: public void removeView(View view)
o Example:

RelativeLayout layout = findViewById([Link]);


Button button = findViewById([Link]);
[Link](button);

3. removeAllViews()
o Description: Removes all child views from the RelativeLayout.
o Syntax: public void removeAllViews()
o Example:

RelativeLayout layout = findViewById([Link]);


[Link]();

4. findViewById(int id)
o Description: Finds a view that was identified by the ID attribute.
o Syntax: public View findViewById(int id)
o Example:

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

Button button = findViewById([Link]);

5. getChildCount()
o Description: Returns the number of child views in the RelativeLayout.
o Syntax: public int getChildCount()
o Example:

RelativeLayout layout = findViewById([Link]);


int count = [Link]();

6. getChildAt(int index)
o Description: Returns the child view at the specified index.
o Syntax: public View getChildAt(int index)
o Example:

RelativeLayout layout = findViewById([Link]);


View child = [Link](0);

7. setOnHierarchyChangeListener(OnHierarchyChangeListener listener)
o Description: Sets a listener to receive notifications about changes in the hierarchy
of child views.
o Syntax: public void
setOnHierarchyChangeListener(OnHierarchyChangeListener listener)
o Example:

[Link](new
[Link]() {
@Override
public void onChildViewAdded(View parent, View child) {
// Handle view added
}

@Override
public void onChildViewRemoved(View parent, View child) {
// Handle view removed
}
});

8. requestLayout()
o Description: Requests that the view be re-laid out.
o Syntax: public void requestLayout()
o Example:

RelativeLayout layout = findViewById([Link]);


[Link]();

9. invalidate()
o Description: Invalidates the view's drawing cache, causing it to be redrawn.
o Syntax: public void invalidate()
o Example:

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

RelativeLayout layout = findViewById([Link]);


[Link]();

10. setGravity(int gravity)


o Description: Sets the gravity used to position children within the layout.
o Syntax: public void setGravity(int gravity)
o Example:

RelativeLayout layout = findViewById([Link]);


[Link]([Link]);

Frame Layout in Android Studio


Definition of Frame Layout
FrameLayout is a type of layout in Android that is designed to block out an area on the screen to
display a single item. It is intended to be used for displaying one view at a time, although
multiple views can be stacked on top of each other. When using FrameLayout, views are placed
in the top-left corner by default, and new views will be drawn on top of existing ones. It is ideal
for simple layouts where you need to manage a single view or overlay multiple views.

Frame Layout Object Inheritance


FrameLayout inherits from ViewGroup, which is a base class for all layout objects in Android.
ViewGroup itself inherits from View, which is the base class for all UI components in Android.

[Link]
↳ [Link]

Applications for Frame Layout


FrameLayout is useful in applications where:

 You want to overlay multiple views on top of each other, such as placing text on top of
an image.
 You need to display a single view at a time, such as in a fragment or a container for
dynamic content.
 You want to implement custom UI elements by layering views, like a floating button or
badge.

Important Properties/Attributes of FrameLayout


1. layout_width
o Description: Defines the width of the FrameLayout.

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

o Syntax: android:layout_width="match_parent" or
android:layout_width="wrap_content"
o Example:

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

2. layout_height
o Description: Defines the height of the FrameLayout.
o Syntax: android:layout_height="match_parent" or
android:layout_height="wrap_content"
o Example:

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

3. layout_gravity
o Description: Defines how the FrameLayout is positioned within its parent.
o Syntax: android:layout_gravity="center"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>

4. foreground
o Description: Sets a drawable resource to be drawn on top of the FrameLayout’s
child views.
o Syntax: android:foreground="@drawable/foreground_drawable"
o Example:

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="@drawable/foreground_drawable"/>

5. padding
o Description: Sets the padding space around the edges of the FrameLayout.
o Syntax: android:padding="16dp"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"/>

6. paddingLeft

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

o Description: Sets the padding space to the left of the FrameLayout.


o Syntax: android:paddingLeft="16dp"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"/>

7. paddingRight
o Description: Sets the padding space to the right of the FrameLayout.
o Syntax: android:paddingRight="16dp"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="16dp"/>

8. paddingTop
o Description: Sets the padding space to the top of the FrameLayout.
o Syntax: android:paddingTop="16dp"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"/>

9. paddingBottom
o Description: Sets the padding space to the bottom of the FrameLayout.
o Syntax: android:paddingBottom="16dp"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"/>

10. layout_margin
o Description: Sets the margin space around the FrameLayout.
o Syntax: android:layout_margin="16dp"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"/>

11. layout_marginLeft

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

o Description: Sets the margin space to the left of the FrameLayout.


o Syntax: android:layout_marginLeft="16dp"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"/>

12. layout_marginRight
o Description: Sets the margin space to the right of the FrameLayout.
o Syntax: android:layout_marginRight="16dp"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"/>

13. layout_marginTop
o Description: Sets the margin space to the top of the FrameLayout.
o Syntax: android:layout_marginTop="16dp"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>

14. layout_marginBottom
o Description: Sets the margin space to the bottom of the FrameLayout.
o Syntax: android:layout_marginBottom="16dp"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"/>

15. layout_centerHorizontal
o Description: Centers the FrameLayout horizontally within its parent.
o Syntax: android:layout_centerHorizontal="true"
o Example:

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

Important Methods of FrameLayout


1. addView(View child)
o Description: Adds a child view to the FrameLayout.
o Syntax: public void addView(View child)
o Example:

FrameLayout frameLayout = findViewById([Link]);


Button button = new Button(this);
[Link](button);

2. removeView(View view)
o Description: Removes a child view from the FrameLayout.
o Syntax: public void removeView(View view)
o Example:

FrameLayout frameLayout = findViewById([Link]);


Button button = findViewById([Link]);
[Link](button);

3. removeAllViews()
o Description: Removes all child views from the FrameLayout.
o Syntax: public void removeAllViews()
o Example:

FrameLayout frameLayout = findViewById([Link]);


[Link]();

4. findViewById(int id)
o Description: Finds a view that was identified by the ID attribute.
o Syntax: public View findViewById(int id)
o Example:

Button button = findViewById([Link]);

5. getChildCount()
o Description: Returns the number of child views in the FrameLayout.
o Syntax: public int getChildCount()
o Example:

FrameLayout frameLayout = findViewById([Link]);


int count = [Link]();

6. getChildAt(int index)
o Description: Returns the child view at the specified index.
o Syntax: public View getChildAt(int index)
o Example:

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

FrameLayout frameLayout = findViewById([Link]);


View child = [Link](0);

7. setOnHierarchyChangeListener(OnHierarchyChangeListener listener)
o Description: Sets a listener to receive notifications about changes in the hierarchy
of child views.
o Syntax: public void
setOnHierarchyChangeListener(OnHierarchyChangeListener listener)
o Example:

[Link](new
[Link]() {
@Override
public void onChildViewAdded(View parent, View child) {
// Handle view added
}

@Override
public void onChildViewRemoved(View parent, View child) {
// Handle view removed
}
});

8. requestLayout()
o Description: Requests that the view be re-laid out.
o Syntax: public void requestLayout()
o Example:

FrameLayout frameLayout = findViewById([Link]);


[Link]();

9. invalidate()
o Description: Invalidates the view's drawing cache, causing it to be redrawn.
o Syntax: public void invalidate()
o Example:

FrameLayout frameLayout = findViewById([Link]);


[Link]();

10. setForeground(Drawable foreground)


o Description: Sets the foreground drawable for the FrameLayout, which is drawn
on top of the children.
o Syntax: public void setForeground(Drawable foreground)
o Example:

FrameLayout frameLayout = findViewById([Link]);


[Link](getResources().getDrawable([Link].f
oreground_drawable));

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

Table Layout in Android Studio


Definition of Table Layout
TableLayout is a type of layout in Android that arranges its children in a grid-like fashion. Each
child view is positioned within a grid of rows and columns. Unlike LinearLayout, which
arranges its children in a single row or column, TableLayout allows you to create complex grid
structures, making it useful for designing forms and other tabular data presentations.

Table Layout Object Inheritance


TableLayout inherits from ViewGroup, which is a base class for all layout objects in Android.
ViewGroup itself inherits from View, which is the base class for all UI components in Android.

[Link]
↳ [Link]

Applications for Table Layout


TableLayout is particularly useful in applications where:

 You need to create forms with multiple rows and columns of input fields.
 You want to present data in a tabular format, such as a settings screen or a configuration
page.
 You need to align views in a grid structure, such as a calendar or a grid of images.

Important Properties/Attributes of TableLayout


1. layout_width
o Description: Defines the width of the TableLayout.
o Syntax: android:layout_width="match_parent" or
android:layout_width="wrap_content"
o Example:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

2. layout_height
o Description: Defines the height of the TableLayout.
o Syntax: android:layout_height="match_parent" or
android:layout_height="wrap_content"
o Example:

<TableLayout
android:layout_width="match_parent"

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

android:layout_height="match_parent">

3. stretchColumns
o Description: Specifies which columns should expand to fill any extra space.
o Syntax: android:stretchColumns="1" (stretches the second column)
o Example:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">

4. shrinkColumns
o Description: Specifies which columns should shrink to fit their contents.
o Syntax: android:shrinkColumns="0" (shrinks the first column)
o Example:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">

5. collapseColumns
o Description: Specifies which columns should be hidden when the layout is
rendered.
o Syntax: android:collapseColumns="2" (collapses the third column)
o Example:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:collapseColumns="2">

6. orientation
o Description: Defines the direction in which child views are laid out. While
TableLayout does not use this directly, it's inherited from ViewGroup.
o Syntax: android:orientation="horizontal" or
android:orientation="vertical"
o Example:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

7. weightSum
o Description: Defines the sum of the weights of all child views. This is inherited
from LinearLayout, and while not directly used by TableLayout, it can be
applied to child views.
o Syntax: android:weightSum="1"

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

o Example:

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">

8. columnStretchable
o Description: Marks which columns are stretchable. This is more relevant to
individual TableRow views.
o Syntax: This is set on TableRow and not directly on TableLayout.
o Example:

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnStretchable="1">

9. columnShrinkable
o Description: Marks which columns can shrink to fit their content. Similar to
columnStretchable, it is used in TableRow.
o Syntax: This is set on TableRow and not directly on TableLayout.
o Example:

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnShrinkable="1">

10. columnCollapsed
o Description: Marks which columns should be collapsed. This is applied to
TableRow views.
o Syntax: This is set on TableRow and not directly on TableLayout.
o Example:

<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnCollapsed="2">

11. gravity
o Description: Defines how child views are aligned within the TableLayout.
o Syntax: android:gravity="center"
o Example:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

12. padding
o Description: Sets the padding space around the edges of the TableLayout.
o Syntax: android:padding="16dp"
o Example:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">

13. paddingLeft
o Description: Sets the padding space to the left of the TableLayout.
o Syntax: android:paddingLeft="16dp"
o Example:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp">

14. paddingRight
o Description: Sets the padding space to the right of the TableLayout.
o Syntax: android:paddingRight="16dp"
o Example:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp">

15. paddingTop
o Description: Sets the padding space to the top of the TableLayout.
o Syntax: android:paddingTop="16dp"
o Example:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp">

Important Methods of TableLayout

1. addView(View child)
o Description: Adds a child view to the TableLayout.
o Syntax: public void addView(View child)
o Example:

TableLayout tableLayout = findViewById([Link]);


Button button = new Button(this);

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

[Link](button);

2. removeView(View view)
o Description: Removes a child view from the TableLayout.
o Syntax: public void removeView(View view)
o Example:

TableLayout tableLayout = findViewById([Link]);


Button button = findViewById([Link]);
[Link](button);

3. removeAllViews()
o Description: Removes all child views from the TableLayout.
o Syntax: public void removeAllViews()
o Example:

TableLayout tableLayout = findViewById([Link]);


[Link]();

4. findViewById(int id)
o Description: Finds a view that was identified by the ID attribute.
o Syntax: public View findViewById(int id)
o Example:

Button button = findViewById([Link]);

5. getChildCount()
o Description: Returns the number of child views in the TableLayout.
o Syntax: public int getChildCount()
o Example:

TableLayout tableLayout = findViewById([Link]);


int count = [Link]();

6. getChildAt(int index)
o Description: Returns the child view at the specified index.
o Syntax: public View getChildAt(int index)
o Example:

TableLayout tableLayout = findViewById([Link]);


View child = [Link](0);

7. setOnHierarchyChangeListener(OnHierarchyChangeListener listener)
o Description: Sets a listener to receive notifications about changes in the hierarchy
of child views.
o Syntax: public void
setOnHierarchyChangeListener(OnHierarchyChangeListener listener)
o Example:

Layouts : Linear, Relative, Frame and Table


Mobile Application Development: Notes on Layouts in Android Studio

[Link](new
[Link]() {
@Override
public void onChildViewAdded(View parent, View child) {
// Handle view added
}

@Override
public void onChildViewRemoved(View parent, View child) {
// Handle view removed
}
});

8. requestLayout()
o Description: Requests that the view be re-laid out.
o Syntax: public void requestLayout()
o Example:

TableLayout tableLayout = findViewById([Link]);


[Link]();

9. invalidate()
o Description: Invalidates the view's drawing cache, causing it to be redrawn.
o Syntax: public void invalidate()
o Example:

TableLayout tableLayout = findViewById([Link]);


[Link]();

10. setStretchAllColumns(boolean stretch)


o Description: Sets whether all columns should stretch to fill available space.
o Syntax: public void setStretchAllColumns(boolean stretch)
o Example:

TableLayout tableLayout = findViewById([Link]);


[Link](true);

Layouts : Linear, Relative, Frame and Table

You might also like