Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RelativeLayout: align View centered horizontal or vertical relative to other view

Tags:

Is it possible to align a view in XML in a RelativeLayout centered horizontal or vertical according another already existing view.

For example: lets say there is something like this:

enter image description here

The second text view should be displayed centered below the first text view:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <TextView         android:id="@+id/textView"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentLeft="true"         android:layout_marginLeft="72dp"         android:text="dynamic text" />      <TextView         android:id="@+id/second"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@id/textView"         android:layout_marginLeft="43dp" <!-- Is there a rule to center it? -->         android:text="centered below text 1" />  </RelativeLayout> 

Is is possible to implement something like that in XML? Is there a rule that i have missed yet? I do not want to calculate the position programmatically

like image 836
sockeqwe Avatar asked May 12 '13 18:05

sockeqwe


People also ask

How do you align an element in the center of a relative layout?

As you said yourself, put both elements inside a RelativeLayout. Then, set the "center_horizontal" property of both elements to true, and then set the "below" property of the green element to the id of the black element. The thing is that center_horizontal centers the elements relative to their parent.

What is difference between LinearLayout and RelativeLayout?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions.

What is the relative layout?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).


2 Answers

I have a much better solution than the accepted one. NO EXTRA NESTING! You can do this by combining two attributes on the smaller view. if you are centering horizontally you can use both align_start & align_end to the bigger view. Make sure the text gravity is centered btw "android:gravity="center". For Vertical alignment use both align_top & align_bottom. below is the modified implementation of your layout.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="43dp" >  <TextView     android:id="@+id/textView"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignStart="@+id/second"     android:layout_alignEnd="@+id/second"     android:gravity="center"     android:text="dynamic text" />  <TextView     android:id="@+id/second"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_below="@id/textView"     android:gravity="center"     android:text="centered below text 1" />  </RelativeLayout> 

No need for unnecessary nesting like the accepted answer.

like image 84
Jessicardo Avatar answered Oct 02 '22 04:10

Jessicardo


Use a separate parent layout for those views and add it in your main layout(can contain other things if you have)

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent" >     <LinearLayout          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:orientation="vertical"          android:gravity="center"          android:layout_marginLeft = "30dp">         <TextView             android:id="@+id/textView"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="dynamic text" />          <TextView             android:id="@+id/second"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="centered below text 1" />     </LinearLayout> ... ... other veiws go here ...  </RelativeLayout> 
like image 34
Noundla Sandeep Avatar answered Oct 02 '22 03:10

Noundla Sandeep