4

I already understand that if I use layout_gravity="center_horizontal" on the text view, then the text will be center. But I don't want to use that because I want to have as little code as possible and the best way to do that is by applying gravity="center_horizontal" to my relative layout. I am also asking this question because I am concerned about even using gravity or layout_gravity with relative layouts at all. As when doing my research I came upon this answer.

Notice the part that says:

Don't use gravity/layout_gravity with a RelativeLayout. Use them for Views in LinearLayouts and FrameLayouts.

Even though it seems pretty apparent to me from the relative layout Android Documentation, which clearly lists gravity as a valid attribute, that Google intended these attributes to be used with relative layouts.

If that quote is correct, how do I center views in relative layouts?

Also, here is my code:

Notice the title is not centered horizontally

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center_horizontal"
    tools:context="com.something">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textAlignment="center"
        android:text="@string/app_title"
        android:layout_marginBottom="@dimen/main_spacing"
        android:textSize="24sp" />

    <AutoCompleteTextView
        android:id="@+id/enterContact"
        android:text="Enter Contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_below="@+id/title"
        android:layout_marginBottom="@dimen/main_spacing" />

</RelativeLayout>

enter image description here

Community
  • 1
  • 1
Lv99Zubat
  • 853
  • 2
  • 10
  • 27

6 Answers6

15

This is a supplemental answer to Gravity and layout_gravity on Android.

View gravity and layout_gravity inside a Relative Layout

My original statement that gravity and layout_gravity should not be used in the subviews of a RelativeLayout was partly wrong. The gravity actually works fine. However, layout_gravity has no effect. This can be seen in the example below. The light green and light blue are TextViews. The other two background colors are RelativeLayouts.

enter image description here

As you can see, gravity works, but layout_gravity doesn't work.

Relative Layout with gravity

My original answer about gravity and layout_gravity dealt with these attributes being applied to the Views within a ViewGroup (specifically a LinearLayout), not to the ViewGroup itself. However, it is possible to set gravity and layout_gravity on a RelativeLayout. The layout_gravity would affect how the RelativeLayout is positioned within its own parent, so I will not deal with that here. How gravity affects the subviews, though, is shown in the image below.

enter image description here

I resized the widths of all the subviews so that what is happening is more clear. Note that the way RelativeLayout handles gravity is to take all the subviews as a group and move them around the layout. This means that whichever view is widest will determine how everything else is positioned. So gravity in a Relative layout is probably only useful if all the subviews have the same width.

Linear Layout with gravity

When you add gravity to a LinearLayout, it does arrange the subviews as one would expect. For example, one could "save code" by setting the gravity of LinearLayout to center_horizontally. That way there is no need individually set the layout_gravity of each subview. See the various options in the image below.

enter image description here

Note that when a view uses layout_gravity, it overrides the LinearLayout's gravity. (This can be seen in the title for the two layouts in the left image. The LinearLayout gravity was set to left and right, but the title TextView's layout_gravity was set to center_horizontally.)

Final notes

When positioning views within a RelativeLayout, the general way to do it is adding things like the following to each view:

  • layout_alignParentTop
  • layout_centerVertical
  • layout_below
  • layout_toRightOf

If one wants to set all the views at once, a LinearLayout would likely be better (or perhaps using a Style).

So to sum up,

  • The layout_gravity does not work for subviews in a RelativeLayout.
  • The gravity of a RelativeLayout does work, but not as one might expect.

Supplemental XML

XML for image "View gravity and layout_gravity inside a Relative Layout":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#e3e2ad" >

        <TextView
            android:id="@+id/tvTop1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:textSize="24sp"
            android:text="Views' gravity=" />

        <!-- examples of gravity -->
        
        <TextView
            android:id="@+id/tvTop2"
            android:layout_below="@id/tvTop1"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:text="left" />

        <TextView
            android:id="@+id/tvTop3"
            android:layout_below="@id/tvTop2"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center_horizontal"
            android:text="center_horizontal" />

        <TextView
            android:id="@+id/tvTop4"
            android:layout_below="@id/tvTop3"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right" />

        <TextView
            android:id="@+id/tvTop5"
            android:layout_below="@id/tvTop4"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center"
            android:text="center" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#d6c6cd" >

        <TextView
            android:id="@+id/tvBottom1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:textSize="24sp"
            android:text="Views' layout_gravity=" />

        <!-- examples of layout_gravity -->
        
        <TextView
            android:id="@+id/tvBottom2"
            android:layout_below="@id/tvBottom1"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="left"
            android:background="#bcf5b1"
            android:text="left" />

        <TextView
            android:id="@+id/tvBottom3"
            android:layout_below="@id/tvBottom2"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:background="#aacaff"
            android:text="center_horizontal" />

        <TextView
            android:id="@+id/tvBottom4"
            android:layout_below="@id/tvBottom3"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="right"
            android:background="#bcf5b1"
            android:text="right" />

        <TextView
            android:id="@+id/tvBottom5"
            android:layout_below="@id/tvBottom4"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:background="#aacaff"
            android:text="center" />

    </RelativeLayout>

</LinearLayout>

XML for image "Relative Layout with gravity":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:background="#e3e2ad" >

        <TextView
            android:id="@+id/tvTop1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:text="Views' gravity=" />

        <!-- examples of gravity -->

        <TextView
            android:id="@+id/tvTop2"
            android:layout_below="@id/tvTop1"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:text="left" />

        <TextView
            android:id="@+id/tvTop3"
            android:layout_below="@id/tvTop2"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center_horizontal"
            android:text="center_horizontal" />

        <TextView
            android:id="@+id/tvTop4"
            android:layout_below="@id/tvTop3"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right" />

        <TextView
            android:id="@+id/tvTop5"
            android:layout_below="@id/tvTop4"
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center"
            android:text="center" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#d6c6cd" >

        <TextView
            android:id="@+id/tvBottom1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:text="Views' gravity=" />

        <!-- examples of layout_gravity -->

        <TextView
            android:id="@+id/tvBottom2"
            android:layout_below="@id/tvBottom1"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:gravity="left"
            android:background="#bcf5b1"
            android:text="left" />

        <TextView
            android:id="@+id/tvBottom3"
            android:layout_below="@id/tvBottom2"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:gravity="center_horizontal"
            android:background="#aacaff"
            android:text="center_horizontal" />

        <TextView
            android:id="@+id/tvBottom4"
            android:layout_below="@id/tvBottom3"
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:gravity="right"
            android:background="#bcf5b1"
            android:text="right" />

        <TextView
            android:id="@+id/tvBottom5"
            android:layout_below="@id/tvBottom4"
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:gravity="center"
            android:background="#aacaff"
            android:text="center" />

    </RelativeLayout>

</LinearLayout>

XML for image "Linear Layout with gravity":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:background="#e3e2ad"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="24sp"
            android:text="View's gravity=" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:text="left" />

        <TextView
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center_horizontal"
            android:text="center_horizontal" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right" />

        <TextView
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center"
            android:text="center" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#d6c6cd"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="24sp"
            android:text="View's gravity=" />

        <TextView
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="left"
            android:text="left" />

        <TextView
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center_horizontal"
            android:text="center_horizontal" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="40dp"
            android:background="#bcf5b1"
            android:gravity="right"
            android:text="right" />

        <TextView
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:background="#aacaff"
            android:gravity="center"
            android:text="center" />

    </LinearLayout>

</LinearLayout>
Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
0

Testing your layout, for me it's working (the TextView is centered correctly).

However, you could also remove android:gravity="center_horizontal" from the RelativeLayout and add android:layout_centerHorizontal="true" in the TextView.

Valentino
  • 2,125
  • 1
  • 19
  • 26
  • hmm, it's working for you? I just tested on my phone and it's not working. It looks like what the last image I posted is showing, where the left side of the title is lined up with the left side of the parent. – Lv99Zubat Feb 09 '17 at 16:48
  • Okay the problem is when I have the autocompletetextview as a child of the relative layout. For whatever reason that kills the gravity setting. I added more code that actually doesn't work this time. – Lv99Zubat Feb 09 '17 at 17:02
  • 1
    Testing with the `android:layout_centerHorizontal="true"` property into the TextView, it's working fine – Valentino Feb 09 '17 at 17:39
  • 1
    yeah, I mean I appreciate your help. I was just seeing if I could figure out why I can't just apply 1 line of code to all the children in the relative layout (i.e. gravity on relative layout) and have everything be center but it's just not working out for me and I guess I just have to accept the fact that android layout design is super glitchy. – Lv99Zubat Feb 09 '17 at 17:48
  • Yes, in this specific case I don't know why it's not working – Valentino Feb 09 '17 at 18:01
  • Note however that if you put some TextView's one below the other (not AutocompleteTextView), the gravity in the RelativeLayout works. – Valentino Feb 09 '17 at 18:04
  • 1
    Note also, that if you set `android:layout_width="wrap_content"` to the AutoCompleteTextView, the gravity works!! So it's a very specifi problem of the AutoCompleteTextView and `android:layout_width="match_parent"` – Valentino Feb 09 '17 at 18:07
  • you're right, but gravity still only works with the top item, the rest of the items are left aligned with the left side of the top item :P – Lv99Zubat Feb 09 '17 at 18:14
  • I mean: if you remove the AutoCompleteTextView, and put other TextViews one below the other, gravity setted on the RelativeLayout level, works on all the TextViews (just tested) – Valentino Feb 09 '17 at 18:16
  • so it seems to only work with textviews, anything else like a button or switch, etc. does that left align thing – Lv99Zubat Feb 09 '17 at 18:20
  • Well, actually it works on all the components except of AutoCompleteTextView (with `android:layout_width="match_parent`). You can try to test with the other components, it should work. Actually, what I guess is that it works on all the components (also with AutoCompleteTextView), under the condition that all have setted `android:layout_width="wrap_content`. – Valentino Feb 09 '17 at 18:26
  • 1
    Hey Valentino, just wanted to let you know a good accepted answer was posted to this question in case you're interested. The important point to know is: "whichever view is widest will determine how everything else is positioned". – Lv99Zubat Feb 13 '17 at 17:56
  • Thanks so much! I appreciate a lot :) I will read it as soon as possible – Valentino Feb 13 '17 at 18:03
0

I've always had problems with RelativeLayouts and TextViews too. I think that this is due to the fact that TextView calculates its size dynamically and this prevents it to work well with layout's gravity. That's just one my supposition. The solution I generally adopt is to configure a textview horizontal size with match_parent. It might be a suboptimal solution, but should work. In case you need a Fixed size textView you can put a fixed size dimension and should work too, it gives problems only with wrap_content.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center_horizontal"
    tools:context="com.something">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textAlignment="center"
        android:text="@string/app_title"
        android:layout_marginBottom="@dimen/main_spacing"
        android:textSize="24sp" />

</RelativeLayout>

Edit: as suggested by @Valentino S., adding

android:layout_centerHorizontal="true"

with wrap_content should work too. The reason is in my head still the same: size of textView is calculated later.

Beppi's
  • 2,089
  • 1
  • 21
  • 38
  • hey, I appreciate your answer but also I edited my code since the stripped down code I added actually was working. It seems that the code gravity gets messed up when I add a autocompletetextview as a child to the relative layout. any thoughts about that? – Lv99Zubat Feb 09 '17 at 17:01
  • LOL :) not really... anyway, keep it as an alternative way, if it's not a solution :) – Beppi's Feb 09 '17 at 17:04
0

try this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:background="#000000"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:background="#ffffffff"
        android:text="TITLE"
        android:textAlignment="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textSize="24sp" />

    <AutoCompleteTextView
        android:id="@+id/enterContact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_marginBottom="10dp"
        android:background="#ffffffff"
        android:text="Enter Contact"
        android:textAlignment="center" />

</RelativeLayout>

enter image description here

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • You just changed 'android:layout_width' of the textview to match_parent. This doesn't answer my question. – Lv99Zubat Feb 09 '17 at 17:12
  • `see how in the last image the title is to the left` the title is now in center of the layout..so what is ur question? – rafsanahmad007 Feb 09 '17 at 17:16
  • The box surrounding the title is the outline of the textview, it should be center in the relative layout. Sorry, I see why that was confusing. – Lv99Zubat Feb 09 '17 at 18:07
0

As the documentation says:

android:gravity

Specifies how an object should position its content, on both the X and Y axes, within its own bounds.

...

center_horizontal

Place object in the horizontal center of its container, not changing its size.

So the android:gravity="center_horizontal" will center horizontal-ing the RelativeLayout to its parent. Not making its content center horizontal.

Remember that android:layout_gravity is used for the view itself relative to the parent or layout.

Please be aware that the behaviour of Layout can differ slightly for each API Level. And be noted, that we can't 100% sure that the visual result in Layout Editor is correct. Always test your layout in real devices.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • from your link, **Specifies how an object should position its content, on both the X and Y axes, within its own bounds.**, I think you missread it :) – Pier Giorgio Misley Feb 09 '17 at 17:07
  • I understood the documentation correctly. Also, I tested on my device and the layout editor seems to match the actual result on my device in this particular case. – Lv99Zubat Feb 09 '17 at 17:07
  • @PierGiorgioMisley: some text below the linked text.. Not found an anchor for it in the documentation :P – ישו אוהב אותך Feb 09 '17 at 17:10
  • @ImpalaTamer: You need to test it with new API Level. I've experience some of the anomalies with layout when upgrading my sdk build version. – ישו אוהב אותך Feb 09 '17 at 17:12
  • not 100% sure, but i think the text about *center_horizontal* refers to the generic meaning of this keyword. The useful part should be the *gravity* description, but I can fail :) – Pier Giorgio Misley Feb 09 '17 at 17:13
  • @ישואוהבאותך I am testing on API 25 which is the latest. – Lv99Zubat Feb 09 '17 at 17:15
  • 1
    @PierGiorgioMisley: Well, you're right. The quoted text give ambiguous. I've update it. – ישו אוהב אותך Feb 09 '17 at 17:17
  • @ImpalaTamer: Looks like I've misunderstood your question. I'm sorry for that, late night here. Answering your question, you need to use `android:layout_gravity="center_horizontal"` for each view or you can try adding another RelativeLayout inside the RelativeLayout with `android:gravity="center_horizontal"` – ישו אוהב אותך Feb 09 '17 at 17:23
0

TlDr: android:gravity Works with RelativeLayout , you can skip to the bottom if you don't want to read the explanation

EDIT this is a giant wall of text, please bear with me

This is what I imagine you want to achieve:

<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textAlignment="center"
        android:text="@string/app_title"
        android:layout_marginBottom="@dimen/main_spacing"
        android:textSize="24sp"
        android:layout_centerHorizontal="true"/>  <!-- LAYOUT PARAMS WORK!!!-->

Why does this work, while android:gravity (apparently) doesn't?

  1. if your goal is to put the TextView in the horizontal center of the Layout/Screen you have to let the RelativeLayout know!

  2. this is achieved through something called LayoutParams - these are data structures defined by each View which are used by the View's parent (in this case the RelativeLayout)

  3. so let's say your TextView has the following LayoutParams :

    android:layout_centerHorizontal="true"

you will get something like this:

enter image description here

When the RelativeLayout is distributing its child views around the screen, along the way there is a callback method being run - onLayout(...) - which is part of a more complex method sequence that will determine the position of each child View inside the RelativeLayout, this is achieved in part by accessing the LayoutParams in each child View, in this case that line in your TextView

  1. This is why we say LayoutParams are passed on to the parent like in the link you mentioned before

WARNING: Endless Confusion Source!

View positioning inside Layouts / ViewGroups is done through LayoutParams the onLayout calls

It so happens that some layouts like FrameLayout have a LayoutParam called android:layout_gravity which causes great confusion with the android:gravity property that each view can define, which is NOT the same and not even a LayoutParam

android:gravity is used by any View (like a TextView for example) to place its content inside.

Example: let's say that you change your TextView to be very high, and you want the text at the bottom of the TextView, instead of at the top

<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="95dp"   <------------ very high!
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textAlignment="center"
        android:text="@string/app_title"
        android:layout_marginBottom="@dimen/main_spacing"
        android:textSize="24sp"
        android:layout_centerHorizontal="true" <----- LayoutParams for RelativeLayout
        />

TextView is CENTERED but text is at the TOP

TextView is CENTERED in the RelativeLayout but text is at the TOP of the "box"

Let's use android:gravity to manage the text position INSIDE THE TextView

<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="95dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textAlignment="center"
        android:text="@string/app_title"
        android:layout_marginBottom="@dimen/main_spacing"
        android:textSize="24sp"
        android:layout_centerHorizontal="true"
        android:gravity="bottom"       <---**NOT LayoutParams**, NOT passed to RLayout
        />

Result: As expected only the inside of the View changed

enter image description here

TLDR Now if you want to ignore everything above and still use android:gravity with RelativeLayout, RelativeLayout is also a Viewso it has the android:gravity property, but you have to remove other properties or LayoutParams which will override the behaviour defined by the android:gravity property look at android:gravity at work with RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="bottom">   <!-- NOT LAYOUT PARAMS -->

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="95dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textAlignment="center"
        android:text="@string/app_title"
        android:layout_marginBottom="@dimen/main_spacing"
        android:textSize="24sp"
        android:layout_centerHorizontal="true"
        />

    <AutoCompleteTextView
        android:id="@+id/enterContact"
        android:text="Enter Contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_below="@+id/title"
        android:layout_marginBottom="@dimen/main_spacing" />

</RelativeLayout>

enter image description here

Community
  • 1
  • 1
HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
  • `gravity` is not a LayoutParams because `gravity`, in a Relative Layout, is not meant to be used to tell a Relative Layout's parent some information. It is meant to tell the Relative Layout's children how they should be aligned within in the Relative Layout. This is why the attribute, `gravity`, exists in [this](https://developer.android.com/reference/android/widget/RelativeLayout.html#attr_android:gravity) Relative Layout documentation. `gravity` applied to a Frame Layout works in the exact opposite fashion as `gravity` applied to a Relative Layout and the documentation accurately shows that. – Lv99Zubat Feb 09 '17 at 18:42
  • @ImpalaTamer yes indeed, as I was saying, android:layout_gravity is a LayoutParam, and is passed to the parent ViewGroup in the hierarchy, android:gravity is used within the View itself - not passed to the parent, also no mention of LayoutParams here ;) – HenriqueMS Feb 09 '17 at 18:47
  • yeah so in my question, I'm having problems with `gravity` applied to a Relative Layout. Any thoughts on the glitches I'm having? – Lv99Zubat Feb 09 '17 at 18:58
  • @ImpalaTamer Alright i was under the impression you had solved the issue and were trying to understand the reason behind it let me have a look at your xml and try to help after dinner – HenriqueMS Feb 09 '17 at 19:30
  • well I actually have a solution that I can move forward with but I really would like to know why `gravity` doesn't work with Relative Layouts so if you have any ideas about that, that would be cool and that's the answer I would consider the "best". – Lv99Zubat Feb 09 '17 at 19:37
  • @ImpalaTamer I have written an in depth explanation, with examples, I hope it helps you understand, take a look at the edited answer. Spoiler: android:gravity works with RelativeLayout, you just shouldnt use it because you have so much better/versatile tools with the RelativeLayout LayoutParams. Cheers! – HenriqueMS Feb 10 '17 at 00:53
  • @ImpalaTamer did you understand the explanation? – HenriqueMS Feb 10 '17 at 19:34
  • I understand your explanation but it doesn't really answer my question as to why gravity of a relative layout wasn't doing what I expected it to do. There is an answer here that says "whichever view is widest will determine how everything else is positioned. So gravity in a Relative layout is probably only useful if all the subviews have the same width." That's what I needed to know to clear my confusion. I really appreciate your effort though. – Lv99Zubat Feb 13 '17 at 15:54