How can I center two text elements vertically in a layout

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

enter image description here

I put the green line in the picture above in the (vertical) center of the parent. TextView is currently in the right place, but I did it as I will describe below using a field and matching with the parent and knowing the size of the parent.

For various reasons, I cannot use only one TextView and center it. In addition, I do not know the height of the parent, so I can not align them with the top / bottom of the parent and lower them up / down to center them.

I also tried to have another View that takes up half the height and will be invisible , but layout_height does not accept percentages (or at least it gave me errors).

Any ideas how I can do this?

Should I just put two TextView inside another layout and have this center?

+6
source share
2 answers

Create a view with a height of 0-height, which is centered in its parent element and serves as an anchor. (Use the android:layout_centerVertical="true" attribute android:layout_centerVertical="true" .) Place one text view above the anchor and the other below the anchor.

+19
source

I solved this problem before using a vertical LinearLayout with two TextViews inside.

ps: Android design guides http://developer.android.com/design/patterns/pure-android.html points to Don't use right-pointing carets on line items . This may not be the position you are doing, but just in case, just indicate it.

+2
source

All Articles