27

I have a RelativeLayout with views aligned relative to it (their parent), using (e.g.):

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setLayoutParams(layoutParams);

and now I wonder, how can I position the View relative to the bottom of its parent, but at an offset (e.g. 100 pixels above the bottom, 20 pixels from the left, etc.), or similarly with the center (30 pixels under the center)?

schematic image of the necessary layout

I've tried setting the margins of the View (e.g.):

layoutParams.setMargins(140, 0, 0, 0);

before applying it to the textView, but that didn't work.

It seems like an extremely useful way to align views for various screen sizes.

It's important for me to achieve this in code, without using xmls.

Thanks!

DannyA
  • 1,571
  • 2
  • 17
  • 28

2 Answers2

64

you have 2 ways, the easies is using a anchor empty view positioned at the center

<View
    android:id="@+id/anchor"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_centerInParent="true" />

<TextView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/anchor"
    android:layout_marginBottom="20dp"
    android:text="hello world!"/>

Or you can center your view and use pading por positioning (you must double the padding used because it overflows below the center of the screen)

 <TextView
     android:id="@+id/imageView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerInParent="true"
     android:paddingBottom="40dp"
     android:text="hello world!"/>
onmyway133
  • 45,645
  • 31
  • 257
  • 263
Jokin
  • 4,188
  • 2
  • 31
  • 30
  • what a resurrection :). Would you say the pros-cons are: anchor method is a bit easier to use but might waste a bit more cpu/memory resources (making the relative layout tree larger)? Also, my original question needed this to be done in code. Is there any more to this than using something like: layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0)? Thanks! – DannyA Sep 25 '12 at 06:29
  • For a simple view where you have a couple of elements this works perfectly! – Vladimir Jul 28 '15 at 09:16
  • I independently came up with the exact same solution, even calling the view "anchor". The only difference is that I set the anchor's height to "match_parent" rather than zero. Is there any advantage to using zero? – F.K. Juliano Dec 02 '21 at 20:34
0

I just started Android programming tonight, so this may be old or a bad idea... but I was tinkering with the above answer and found positioning an element below my other element was causing a problem - either the padding would be in the way or something. Here's what I ended up doing.

Using the anchor technique, I also gave the anchor a height (double what you need), and then aligned my element with the top. That way it'd be in the middle of the view minus half the height of the anchor!

<View
    android:id="@+id/anchor"
    android:layout_width="0dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true" />

<TextView
    android:id="@+id/heading"
    android:text="@string/heading"
    android:textColor="@android:color/white"
    android:textSize="24sp"
    android:layout_alignTop="@id/anchor"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/subheading"
    android:text="@string/subheading"
    android:textColor="@android:color/white"
    android:textSize="12sp"
    android:layout_below="@+id/heading"
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

The other answer helped a lot but I just wanted to give my 2 cents!

Tom Prats
  • 7,364
  • 9
  • 47
  • 77