Home > front end >  Horizontally center two TextView with RelativeLayout
Horizontally center two TextView with RelativeLayout

Time:12-18

Problem:

Im trying to horizontally center these two TextView, but can't seem to find a solution. enter image description here

    <TextView
    android:id="@ id/have_account_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@ id/register_btn"
    android:layout_marginTop="15dp"
    android:text="Already have an account? "
    android:textColor="@color/black" />

<TextView
    android:id="@ id/login_now_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textFontWeight="700"
    android:textColor="@color/main_blue"
    android:layout_below="@ id/register_btn"
    android:layout_marginTop="15dp"
    android:layout_toRightOf="@ id/have_account_tv"
    android:text="Log in now" />

Solution

I've solved this using LinearLayout wrapping both TextView, and then set centerHorizontal="true".

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:layout_below="@ id/login_btn">
    <TextView
        android:id="@ id/havent_register_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Haven't Registered? "
        android:textColor="@color/black" />

    <TextView
        android:id="@ id/login_now_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textFontWeight="700"
        android:textColor="@color/main_blue"
        android:layout_alignBottom="@ id/havent_register_tv"
        android:layout_toRightOf="@ id/havent_register_tv"
        android:text="Register Here"/>
</LinearLayout>

CodePudding user response:

Try adding following attribute to the TextView XMLs

android:layout_centerHorizontal="true"
  • Related