5

I have a RelativeLayout (that I am not married to) and two TextViews. I would like the bottom of the first TextView to line up with the center of it's parent, and the top of the second TextView to line up with the center of it's parent.

enter image description here

I put a green line in the picture above at the (vertical) center of the parent. The TextViews are currently in exactly the right spot, but I did this as I will describe below with margin's and aligning them to the parent and knowing the parent's size.

For various reasons I can't use just one TextView and center it. Also I don't know the height of the parent, so I can't align each to the top/bottom of the parent and margin them down/up to center them.

I also tried having another View that takes up half the height and be invisible, but layout_height doesn't take percents (or at least it gave me errors).

Any ideas how I can accomplish this?

Should I just put the two TextViews inside another layout and have that center itself?

1

2 Answers 2

21

Create a 0-height view that is centered in its parent and serves as an anchor. (Use the attribute android:layout_centerVertical="true" for this.) Position one text view above the anchor and the other below the anchor.

3
  • Brilliant, 0 height, instead of 50%... Very clever. Thanks
    – xbakesx
    Dec 19, 2012 at 19:56
  • see my answer no need to create new textview stackoverflow.com/questions/22161456/…
    – blackHawk
    Feb 20, 2018 at 10:53
  • @blackHawk - True, I'm creating a new view, but what new textview are you talking about? Also, the solution you posted won't work here because, as OP pointed out in the question, the height of the parent isn't known at coding time, so it's impossible to know what margins to use to get the correct vertical centering.
    – Ted Hopp
    Feb 20, 2018 at 14:08
2

I've solved this problem before by using a vertical LinearLayout with the two TextViews inside it.

ps.: the Android Design Guidelines http://developer.android.com/design/patterns/pure-android.html specific points out to Don't use right-pointing carets on line items. It might not be a line item that you're doing, but in case it is, just to point it out.

3
  • Thanks, that will totally work. I am not sure which is worse adding an empty view as an anchor or wrapping more layouts in there... Also appreciate the comment on the carets. I just noticed this last week in their design guidelines.
    – xbakesx
    Dec 19, 2012 at 19:57
  • Ignoring the comment re. caret usage, how was the problem of centering the textViews in the LL actually solved?
    – greg7gkb
    Sep 17, 2013 at 18:14
  • @greg7gkb - The LL height is set to wrap_content and the LL is then centered vertically in the parent RelativeLayout. This aligns the midpoint of the block of two TextViews rather than the baseline. It's the option that OP had suggested in the last line of the question.
    – Ted Hopp
    Oct 26, 2015 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.