Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center View below another View

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>
like image 554
Salo Avatar asked Oct 27 '11 15:10

Salo


People also ask

How do I center a view in another view?

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.

How do I center a view in constraint layout?

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.

How do you center text in view?

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.


1 Answers

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" />
like image 103
Galya Avatar answered Oct 13 '22 20:10

Galya