0

There is no layout_gravity attribute in RelativeLayout class and RelativeLayout.LayoutParams inner class. It only has android:gravity attribute. But still I am able to add layout_gravity attribute to RelativeLayout in xml file and make the layout change according to its parent. Look at the following code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="false"
    android:background="@color/colorPrimaryDark"
    android:text="First Child"
    android:textAppearance="?android:textAppearanceLarge" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/textView"
    android:layout_toRightOf="@+id/textView"
    android:background="@color/colorPrimaryDark"
    android:text="Second child"
    android:textAppearance="?android:textAppearanceLarge"/>

When I run this code, the RelativeLayout positions itself in the centre of the screen. I thought it will throw an error since RelativeLayout does not have layout_gravity attribute. So how is it possible to use layout_gravity in RelativeLayout?

Here's the link for RelativeLayout documentation https://developer.android.com/reference/android/widget/RelativeLayout.html. I searched for layout_gravity in it. But there's no layout_gravity in RelativeLayout.

  • `android:layout_gravity` is the gravity of the RelativeLayout itself. try using `android:centerInParent = "true"` for child views – ColonD Mar 07 '18 at 12:23
  • yeah, but my question is how layout_gravity can be used in RelativeLayout. RelativeLayout does not have such attribute. – PAZHAMALAI M UEE14227 Mar 07 '18 at 12:29
  • yes it has that attribute every possible independent views and widgets have that attribute in android !! @PAZHAMALAIMUEE14227 – Santanu Sur Mar 07 '18 at 12:36
  • `layout_gravity` is a property of the `View` class from which `RelativeLayout` is a subclass. Hence `RelativeLayout` also has all properties of `View` class – ColonD Mar 07 '18 at 12:40
  • `layout_gravity` is only for the `linearLayout` first you need to read the `attributes` of layout – Vishal Yadav Mar 07 '18 at 13:05
  • Hope this helps: I mean, that every property with the prefix "layout_something" is a property related to the parent view - like ColonD states... I did a quick test, and if you check out the parent of your RelativeLayout, you will find that this is either a FrameLayout or a LinearLayout (if the RelativeLayout is the root of your activity layout)... I am testing on an "AppCompatActivity" and the parent is a "android.support.v7.widget.ContentFrameLayout" - meaning, that its subviews can use layout_gravity... FYI. this is not the case for a simple "View"... sorry for the long description. – Mathias Kirkegaard Mar 07 '18 at 13:31
  • Thanks @MathiasKirkegaard. But could you please tell me how to find the parent to my RelativeLayout? – PAZHAMALAI M UEE14227 Mar 07 '18 at 13:45
  • First, give your RelativeLayout an id... In your activity class, find your relativelayout using the id... and then you can check "Parent" (or "getParent" in java i think - I use Xamarin), and then you can check type of the parent? – Mathias Kirkegaard Mar 07 '18 at 13:50
  • "If you like it, then you should have put a ring on it"... cough, mark it as useful :) – Mathias Kirkegaard Mar 08 '18 at 09:33

0 Answers0