Layouts in Android
The layout is part of the AndroidJetpack. In android, we usually used an XML based layout.
An XML based layout provides a structure for the user interface in the application. The elements in the layout are using a hierarchy of
graphical representation of the view of hierarchy, define a Layout
Note: image reference from developer.android.com
Layout Declaration:
In 2-ways we can declare a layout
- Declare UI elements in XML: this is the simplest and most common way to represent a layout
- Initialize the layout at runtime: We can also initialize a layout programmatically in our application.
Write XML:
Using XML, you can design UI layout quickly. You must have only one root or parent element in each layout. Under that root element, you can easily hierarchically add various layout objects.
Example:
here’s, in an XML layout uses a vertical
layouts in the XML file save as
Load XML:
In activity.onCreate() callback method you load your layout XML file with setContentView(), and passing the layout resource in the form of R.layout.layout_file_name as a reference.
For example
Common Layout:
ViewGroup class provides some way to display the views, some of the common layout types are:
- Linear Layout:
Other Layouts:
There are some layouts for which you need to use an Adapter. ListView and GridView. Because these layouts are dynamic, you can’t determine these layouts. For that, you need to use a subclass of the AdapterView class an Adapter. The Adapter holds the data in the form of an array or database query and converts data into a view.
- ListView:
- GridView:
Quick Recap:
- The layout is part of the AndroidJetpack. In android, we usually used an XML based layout.
- The elements in the layout are using a hierarchy of View and ViewGroup objects.
- 2-ways we can declare a layout.
- The layout has only one root or parent element in each layout.
- In activity.onCreate() callback method you load your layout XML file with setContentView(), and passing the layout resource in the form of R.layout.layout_file_name as a reference.