What is Android layout?
Layout denotes the architecture of the application, where and how the controls should be organized in the UI.It defines the layout structure and holds all the elements that appear to the user.
Android allows us to create layouts for the applications using simple XML file and those layouts must be placed in /res/layout folder.
We can declare the layout in two ways and here we will discuss first way of doing it:
- Declare UI elements in XML
- Create UI elements at runtime using Java.
- Linear Layout
- Relative Layout
- Table Layout
- WebView Layout
- Grid View
- Tab Layout
- List View
Linear layout
In a linear layout, as the name suggests, all the controls are displayed in a linear/sequential fashion, either Horizontally or Vertically and this behavior is set in android:orientation attribute of the node LinearLayout.
android:orientation
=
"vertical"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
</
LinearLayout
>
LinearLayout – designed to display child View controls in a single row or column. So it is a very handy layout method for creating forms.
Layout architecture:
New Visitor? Like what you read? Then do subscribe to our blog
Subscribe
Subscribe
1.Parent layout
2.Child layout.
In parent layout, the controls are organized within a vertically oriented linear layout and in child layout, the controls are organized within a horizontally oriented linear layout.
Create layout XML
- Create new android project [File >> New >> Android Project] with project name LinearLayoutExample
- Click next and select target android device version [I chose version 2.2]
- Click next and enter package name
- Click finish
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
EditText
android:id
=
"@+id/editText1"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:ems
=
"10"
android:hint
=
"Username"
>
<
requestFocus
/>
</
EditText
>
<
EditText
android:id
=
"@+id/editText2"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:ems
=
"10"
android:hint
=
"Password"
android:inputType
=
"textPassword"
/>
<
Button
android:id
=
"@+id/button1"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:text
=
"Button"
/>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
TextView
android:id
=
"@+id/textView1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Gender"
android:textAppearance
=
"?android:attr/textAppearanceMedium"
/>
<
RadioButton
android:id
=
"@+id/radioButton1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Male"
/>
<
RadioButton
android:id
=
"@+id/radioButton2"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"Female"
/>
</
LinearLayout
>
</
LinearLayout
>
Click the below image and take a look at the way the controls are arranged for the XML we added above:
- Two EditView controls(Username and password) and button control are arranged in vertical fashion since the android:orientation attribute of the node LinearLayout is set to ‘vertical’
- TextView(Gender) and two radio buttons(Male and Female) are arranged horizontally since the android:orientation attribute of the node LinearLayout is set to ‘horizontal’
Let us test the application:
Run click on the project >> Run as >> Android application
You could see following screen:
Entire
project is zipped and is available for download. Unzip the downloaded
project and to import the project into eclipse, launch eclipse >>
File >> Import.. >> Choose downloaded project(How to import android project in eclipse).
If you just want to run the application in your mobile and see the
output but don’t want to hit your head with source code, download
application(apk) file and install it in your mobile device. Download Source CodeDownload Application(apk)*apk in Android is the installation file simliar to exe in windows.
0 nhận xét:
Post a Comment