Ticker

6/recent/ticker-posts

Layouts in Android

Layouts in Android

Hi everyone,
In this blog of Android Tutorial, we are going to discuss various Layout of Android. The layout is part of the AndroidJetpack. In android, we usually used an XML based layout.

Layouts in Android


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
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
  1. Declare UI elements in XML: this is the simplest and most common way to represent a layout
  2. 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:
Horizontal Linear Layout View
Horizontal Linear Layout View

Vertical Linear Layout View
Vertical Linear Layout View
Relative Layout View
Relative Layout View

WebView representation
WebView representation


Other 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:
Listview representation
Listview representation

  • GridView:
GridView representation
GridView representation





**Comment below if you have any queries.



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.

Post a Comment

0 Comments