Android Layouts

In Android, layouts define structure for the user interface shown on the window of the device. The structure in the layouts are built using Views and ViewGroups. These are the building block of any UI elements shown on the screen.

View: Refers to the element which the user can touch and interact with.

ViewGroup: Refers to an invisible component which contains Views, or other ViewGroups.

Below are some predefined Views and ViewGroups available in Android Library:

Views: Button, TextView (all these extend View class)

ViewGroups: LinearLayout, ConstraintLayout, RelativeLayout and many others (all these extend ViewGroup class)

What should you look for when defining layouts?

Layouts are a very important part of Android applications. The devices we use do not have unlimited memory, battery or similar other resources. Everything is limited. So designing layouts in an optimized way helps in making efficient use of available these resources. If implemented poorly your layout can lead to a memory hungry application with slow UIs and which directly impacts the user experience.

To achieve better layout performance we need to keep looking to optimize our layouts. A few ways we can achieve:
  • Always strive to achieve flatter hierarchies. Having complex, deeper hierarchy will cause a lot performance issues
  • Make UIs as reusable components. Use <include/>, <merge/> tags to avoid deeper hierarchy
  • To load views only when required i.e at runtime.

How to define a layout in the project?

Using XML

a. Defining a layout

All the layouts defined using XML are declared in the "res/layout" directory of the project. Each of those files have .xml as extension.



Diagram explanation

<androidx.appcompat.widget.LinearLayoutCompat
This is the root tag of this layout which is ViewGroup. Each layout file has exactly one root element. This just acts as a container to other views and view groups.

<com.google.android.material.button.MaterialButton
This is a view which extends the View class and part of Android material library (it can be easily understood from the path part of the view tag.

<FrameLayout
This is a view group which extends the ViewGroup class and part of the Android library.


b. Load the layout

To be able to show the UI to the user the defined layout needs to be loaded into the app code. Here is an example on how layout is loaded into the Activity component. Check the “setContentView” method.


Using code (dynamically added a View)

a. Creating a new View object

To create a new view dynamically the first step is to create a new object of the view class. In this example we have created a MaterialButton object using its constructor and have set its ID and text attribute.


b. Loading new view object into the ViewGroup

To let the newly created view object visible to the user we will have to add it to new ViewGroup or any existing view group defined in the XML or using code. Here in this example we are adding the new view object in the existing ViewGroup declared in the XML file with id "bodyLayout".

Resources

Comments

Popular posts from this blog

Fragment Lifecycle

AndroidManifest

Activity Lifecycle