I am using a RelativeLayout in order to place a View. Now i want to add one more View that is below the first View, and shall be centered referred to the first View. But there seems to be no option thats called something like android:layout_alignCenter.. How can i achieve this task?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:gravity="top">
<TextView
android:text="Blaa"
android:id="@+id/TextView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="100dp"
android:gravity="center"
/>
<TextView
android:text="Foo"
android:id="@+id/TextView2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/TextView1"
--- NEED STH THAT ALIGNS THIS SECOND VIEW CENTRAL UNDER THE FIRST VIEW --
/>
</RelativeLayout>
If your views in RelativeLayout follow these steps: 1- wrap your view that wants to be aligned in the center of target view in Framelayout. 2- move all layout attributes to FrameLayout. 3- set layout_align_top and layout_align_bottom (or start and end if horizontal) to target view.
You can use layout_constraintCircle for center view inside ConstraintLayout . with constraintCircle to parent and zero radius you can make your view be center of parent.
android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.
This is an old question, but still unanswered, so...
You can have both the beginning and the end of both views aligned in the same way. This will override the width property of the second one and will make it wide as the first, but will also move it with the first one. If you want to center the second view, you may use gravity or padding. The second view goes so:
<TextView
android:id="@+id/TextView2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/TextView1"
android:layout_alignLeft="@+id/TextView1"
android:layout_alignRight="@+id/TextView1"
android:gravity="center"
android:text="Foo" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With