Android Studio Linear Layout Guide
Android Studio Linear Layout Guide
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.
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"
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:
<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>
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
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>
1. setOrientation(int orientation)
o Description: Sets the orientation of the LinearLayout to either horizontal or
vertical.
o Syntax: [Link]([Link]);
o Example:
2. getOrientation()
o Description: Returns the current orientation of the LinearLayout.
o Syntax: int orientation = [Link]();
o Example:
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:
3. getGravity()
o Description: Retrieves the current gravity setting of the LinearLayout.
o Syntax: int gravity = [Link]();
o Example:
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:
5. getWeightSum()
o Description: Retrieves the current weight sum of the LinearLayout.
6. setDividerDrawable(Drawable divider)
o Description: Sets a drawable as the divider between child views.
o Syntax:
[Link](getResources().getDrawable([Link]
r));
o Example:
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:
8. setDividerPadding(int padding)
o Description: Sets padding around the dividers.
o Syntax: [Link](8);
o Example:
9. setBaselineAligned(boolean baselineAligned)
o Description: Sets whether the LinearLayout aligns its children by their baselines.
o Syntax: [Link](false);
o Example:
10. isBaselineAligned()
o Description: Checks whether the LinearLayout aligns its children by their
baselines.
o Syntax: boolean isBaselineAligned = [Link]();
o Example:
[Link]
↳ [Link]
<RelativeLayout
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"
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"
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"
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"/>
1. addView(View child)
o Description: Adds a child view to the RelativeLayout.
o Syntax: public void addView(View child)
o Example:
2. removeView(View view)
o Description: Removes a child view from the RelativeLayout.
o Syntax: public void removeView(View view)
o Example:
3. removeAllViews()
o Description: Removes all child views from the RelativeLayout.
o Syntax: public void removeAllViews()
o Example:
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:
5. getChildCount()
o Description: Returns the number of child views in the RelativeLayout.
o Syntax: public int getChildCount()
o Example:
6. getChildAt(int index)
o Description: Returns the child view at the specified index.
o Syntax: public View getChildAt(int index)
o Example:
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:
9. invalidate()
o Description: Invalidates the view's drawing cache, causing it to be redrawn.
o Syntax: public void invalidate()
o Example:
[Link]
↳ [Link]
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.
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
<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
<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"/>
2. removeView(View view)
o Description: Removes a child view from the FrameLayout.
o Syntax: public void removeView(View view)
o Example:
3. removeAllViews()
o Description: Removes all child views from the FrameLayout.
o Syntax: public void removeAllViews()
o Example:
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:
5. getChildCount()
o Description: Returns the number of child views in the FrameLayout.
o Syntax: public int getChildCount()
o Example:
6. getChildAt(int index)
o Description: Returns the child view at the specified index.
o Syntax: public View getChildAt(int index)
o Example:
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:
9. invalidate()
o Description: Invalidates the view's drawing cache, causing it to be redrawn.
o Syntax: public void invalidate()
o Example:
[Link]
↳ [Link]
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.
<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"
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"
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">
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">
1. addView(View child)
o Description: Adds a child view to the TableLayout.
o Syntax: public void addView(View child)
o Example:
[Link](button);
2. removeView(View view)
o Description: Removes a child view from the TableLayout.
o Syntax: public void removeView(View view)
o Example:
3. removeAllViews()
o Description: Removes all child views from the TableLayout.
o Syntax: public void removeAllViews()
o Example:
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:
5. getChildCount()
o Description: Returns the number of child views in the TableLayout.
o Syntax: public int getChildCount()
o Example:
6. getChildAt(int index)
o Description: Returns the child view at the specified index.
o Syntax: public View getChildAt(int index)
o Example:
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:
9. invalidate()
o Description: Invalidates the view's drawing cache, causing it to be redrawn.
o Syntax: public void invalidate()
o Example: