Android Layouts:Linear and Relative
Link will be apear in 15 seconds.
Well done! you have successfully gained access to Decrypted Link.
A layout defines what a screen looks like and we define it using XML. The Layouts consists of widgets like TextView, EditText, Buttons. The user interacts with the layout to do some task or to perform some functionality.
Two Key Layouts: Linear and Relative
1)Linear: A Linear layout either displays its view in the horizontal or vertical direction. If the linear layout is vertical, it will be displayed in a single column or if the linear layout is horizontal, it will be displayed in the form of a row.
It is defined by using a tag:
There are three necessary attributes that must be initialized android:layout_width, android:layout_height and android: orientation.
The width and height attribute can take a predefined value like match_parent(occupy the full width and height of the parent) or wrap_content(occupy only that width and height in which it can fit itself) or you can specify any integer value.
orientation can take two values horizontal or vertical.
The widget will appear in the same order in which we put widgets in a linear layout.
There is no relative positioning between the widgets.
We only specify the id of the widgets in a linear layout for which we are going to refer to our activity code.
We use gravity attribute to specify where widgets in a layout.
Ex: android:gravity="top"
2)Relative Layout
It displays its views in relative to each other. We need to define the position of each view relative to other views in the layout or relative to the parent.
There are two necessary attributes that must be initialized android:layout_width, android:layout_height.
Code:
Aligning the view relative to parent:
Ex:
1) android:layout_alignParentTop="true"
2) android:layout_alignParentRight="true
We use the id of the widgets to relatively to other widgets.
Ex:
<Button
android:id="@+id/button_click_me"
......
/>
<Button
android:layout_below="@+id/button_click_me"
android:layout_alignLeft="@+id/button_click_me"
......
/>
Two Key Layouts: Linear and Relative
1)Linear: A Linear layout either displays its view in the horizontal or vertical direction. If the linear layout is vertical, it will be displayed in a single column or if the linear layout is horizontal, it will be displayed in the form of a row.
It is defined by using a tag:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:gravity="center_vertical" android:id="@+id/helloText"/> </LinearLayout>
There are three necessary attributes that must be initialized android:layout_width, android:layout_height and android: orientation.
The width and height attribute can take a predefined value like match_parent(occupy the full width and height of the parent) or wrap_content(occupy only that width and height in which it can fit itself) or you can specify any integer value.
orientation can take two values horizontal or vertical.
The widget will appear in the same order in which we put widgets in a linear layout.
There is no relative positioning between the widgets.
We only specify the id of the widgets in a linear layout for which we are going to refer to our activity code.
We use gravity attribute to specify where widgets in a layout.
Ex: android:gravity="top"
2)Relative Layout
It displays its views in relative to each other. We need to define the position of each view relative to other views in the layout or relative to the parent.
There are two necessary attributes that must be initialized android:layout_width, android:layout_height.
Code:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:layout_centerInParent="true" android:id="@+id/helloText" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/helloText" android:text="This is below Hello World text" android:layout_centerInParent="true"/> </RelativeLayout>
Aligning the view relative to parent:
Ex:
1) android:layout_alignParentTop="true"
2) android:layout_alignParentRight="true
We use the id of the widgets to relatively to other widgets.
Ex:
<Button
android:id="@+id/button_click_me"
......
/>
<Button
android:layout_below="@+id/button_click_me"
android:layout_alignLeft="@+id/button_click_me"
......
/>