Android – RelativeLayout

Android RelativeLayout –

RelativeLayout is much more complex than any other Layout , where we do not have to position our various UI View Controls in a linear manner, but rather position them in each other’s Relation, due to which one of the Layout’s UI The change that is or will be done in the positioning of the control has an effect on the positioning of other UI controls placed before and after that UI control.

RelativeLayout really any Default Layout of the Android App, so we do that whenever a new Android App Create using Android Studio are, or whenever a new Activity Create, we received Activity associated with XML Layout File of The Default Layout Element is <RelativeLayout… /> only.

The task of managing Relative Layout is done by RelativeLayout Class and this class determines how different UI Controls of Android App Layout created using this Layout will position the screen in relation to each other. . Therefore, the XML Definition of this Layout has more Parameters and Layout Variables than any other Layout, due to which the XML Codes of this RelativeLayout can be more than twice the XML Codes of LinearLayout .

When we use LinearLayout and due to nesting of many levels, our LinearLayout starts to be quite typical, then developing the same Layout as RelativeLayout becomes very useful in terms of performance because by using RelativeLayout we can use a Single Level In the Hierarchy, we can develop the same Layout, which if we use LinearLayout to develop, we may need to nesting several Levels.

As a result, although RelativeLayout’s XML Codes in a normal Android App may be twice as large as LinearLayout’s XML Codes, we can also write very few XML Codes compared to XML Codes written for very complex LinearLayout Nested across multiple levels. The same can be obtained by Layout and this can be done only through RelativeLayout.

That’s why the Android SDK provides Relative Layout as Default Layout, because by learning how to manage it properly we can create more manageable and efficient Layout through fewer XML Codes than any other Layout.

However it is important to keep in mind that the layout that can be created by a Simple LinearLayout is also foolish to develop using RelativeLayout because the work that can be done by LinearLayout using at least XML Codes , The same Layout should not develop through double XML Codes in RelativeLayout.

This is why the Android SDK provides us with a variety of Layout Options so that we can select the best possible Layout according to our needs, and at least Layout can be developed by XML Codes. If your Layout can be created by 10 Lines of XML Codes using the LinearLayout Element, it is by no means appropriate to write an XML Code of 15 Lines using the RelativeLayout Element to the same Layout. But if the same Layout can only be created by a 5 Line XML Code using a GridView or ListView Element, then that Layout must be created via a ListView or GridView Layout.

RelativeLayout is a ViewGroup Container that always uses less memory than any other layout and performs more smoothly and quickly. This Layout allows us to Arrange all types of UI Views in a single Single Container so that we can create more Complex Layouts through a Single ViewGroup Container and achieve better performance.

As we said earlier, RelativeLayout is a layout under which different UI Controls i.e. Child Views are related with each other, so how will different Siblings position each other in Left or Right or Up or Down and any one It is very important to keep in mind how the other controls will position when the UI control is positioned and therefore RelativeLayout is more complex than any other Layout.

Relative Layout is considered to be the most powerful Layout because it has the ability to Eliminated ViewGroup Nesting of many Levels and gives us such a facility that we can do our entire User Interface Design using the Hierarchy of the same Level, due to which The performance of our Android App becomes very good. So while creating your UI Layout, if you feel that you are nesting LinearLayout of multiple levels, then it is better to modify your UI Layout based on RelativeLayout.

Since when we create a new Android App or add a new activity to the current Android App, the Layout created by default is RelativeLayout. So we do not need to do anything special to change the layout in our Android App.

However if you have created LinearLayout or any other Layout by changing this RelativeLayout, then to convert it to RelativeLayout we have to open the XML Layout File related to our Activity File in XML Code Mode and change the Layout Element < RelativeLayout… /> has to be done. If you look at the RelativeLayout Code that is created by default, then it is as follows:

// File Name: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

      android:text="Hello World!" />


Provides the facility for its Child Elements to be positioned in the context of their Parent or other Siblings, while the ID is used to identify the Child Element to be positioned. So we can Align two different View Elements simultaneously on the same side, position each other upside-down or any UI Element in many ways like Center, Centered Left, Centered Right etc. Align can.

Android RelativeLayout

Since all Child View Elements start to draw from the Top-Left of RelativeLayout by default, we need to specify several Positioning Properties Defined in RelativeLayout.LayoutParams Class for different UI Elements to properly position different UI Elements. it happens.

Since all these properties are related to each other, defining the positioning of a specific element, to understand all of them properly, we need to understand the basic concept of RelativeLayout properly, according to which each UI in this layout View Control is the position on the screen with respect to two other View Controls:

  • With reference to your Parent or Container ViewGroup Element and
  • With reference to your other Sibling View Element.

So when we are creating our Layout using RelativeLayout, we need to specify android: id Attribute in each of our View Element as we can see the current View Object in the context of which other View or ViewGroup Object is on the screen. Where you want to position is determined by those other UI Controls ie View Object android: id Attribute.

When we are creating our Layout using RelativeLayout, the first thing we need to do is to specify <RelativeLayout… /> Element as its Root Element with its three Compulsory Attributes xmlns, layout_height and layout_width . Also, if the Layout appears on the screen properly, it is necessary that we set the four Paddings of this Layout with some value.

When we create a new Android App or a new activity, the Create Layout XML file contains the default <RelativeLayout… /> Element as the default and all these Attributes are already set.

Apart from these, some new Attributes related to the Start and End of Layout Flow based on the concept of Layout’s Text Direction from Android 4.2 + have been added, so that we can easily Internationlize and Localize our Android App . Therefore, it is important to understand these properly before proceeding so that we can better design and control our RelativeLayout.