Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to RelativeLayout Align Center android

<?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:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="78dp"
        android:layout_marginTop="199dp"
        android:text="test" />

     <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:text="I want to align Center" />

</RelativeLayout>

Here is my xml code.

I want to use layout_alignCenter="@+id/textView1" in TextView2.

But there is no alignCenter.

I think layout_centerVertical or centerinParent also useless.

like image 990
Barb_ggul Avatar asked Jun 23 '14 23:06

Barb_ggul


People also ask

How do I center text in RelativeLayout android?

android:gravity controls the appearance within the TextView. So if you had two lines of text they would be centered within the bounds of the TextView. Try android:layout_centerHorizontal="true" . Notice that this won't work if a RelativeLayout contains an attribute android:gravity="center_horizontal".

How android RelativeLayout works?

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).

How do I set gravity center in RelativeLayout?

To center something in a RelativeLayout, you use android:layout_centerInParent="true" on the child. If you try to center several childs, they'll end up under/over each other.


2 Answers

You wanted to show text view exact center of the screen....then use below modified code.

<?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:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_centerInParent="true"
    android:text="test" />

 <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:text="I want to align Center" 
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_centerInParent="true"/>

</RelativeLayout>

This will definitely work for you. If there is any other requirement then explain in detail.

like image 195
Anil Jadhav Avatar answered Oct 23 '22 10:10

Anil Jadhav


You could wrap a the RelativeLayout in a LinearLayout and use Space elements:

<LinearLayout layout_width="0dp" layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
    <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

    <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="78dp"
            android:layout_marginTop="199dp"
            android:text="test" />

         <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:text="I want to align Center" />

    </RelativeLayout>
    <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
</LinearLayout>

The Space element will distribute empty space relatively between all Space elements, based on the layout weights.

like image 2
aeskreis Avatar answered Oct 23 '22 12:10

aeskreis