Android Textview is moved to right

Viewed 85

This is what I actually have as a result: enter image description here

As you can see value to right of Current HP is moved to right instead to stay close to "Current HP" textview like the rest of values. Why is that so? This is my code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@color/semiTrans"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/llHeader"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/text"
            android:layout_marginRight="5dp"
            app:srcCompat="@android:drawable/ic_dialog_info" />

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical|center_horizontal"
            android:text="CONTINUE INFO"
            android:textColor="#000"
            android:textSize="25sp" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl2"
        android:layout_marginHorizontal="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/llHeader"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true">


        <TextView
            android:id="@+id/currentHp"
            android:textStyle="bold"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:text="Current HP: "
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/nextHp"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/currentHp"
            android:text="Next +10HP: "
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/fullHp"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nextHp"
            android:text="HP fully recovered: "
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/currentHpValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/currentHp"
            android:layout_toRightOf="@+id/currentHp"
            android:text="a"
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/nextHpValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/currentHp"
            android:layout_toEndOf="@+id/nextHp"
            android:layout_toRightOf="@+id/nextHp"
            android:text="a"
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/fullHpValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/fullHp"
            android:layout_alignBottom="@+id/fullHp"
            android:layout_toEndOf="@+id/fullHp"
            android:layout_toRightOf="@+id/fullHp"
            android:text="a"
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/tvAdditionalInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@id/fullHp"
            android:textSize="18sp"
            android:layout_centerHorizontal="true"
            android:text="Additional info"/>

    </RelativeLayout>

    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rl2"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginHorizontal="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/button"
        android:text=" Ok " />

</RelativeLayout>

I was trying to solve this by setting padding and margin to 0 but it did not work. I do not know what else Ishould added.

3 Answers

I found a solution. I need to set currentHp textview width to wrap content.

Your current Hp has a width 168 dp. For this reason it is taking that place and after that place your a text is showing.

So you have make the width of current Hp to wrap-content . Then the text a will stay like others.

You can check this code to solve the issue

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@color/semiTrans"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/llHeader"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/text"
            android:layout_marginRight="5dp"
            app:srcCompat="@android:drawable/ic_dialog_info" />

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical|center_horizontal"
            android:text="CONTINUE INFO"
            android:textColor="#000"
            android:textSize="25sp" />


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl2"
        android:layout_marginHorizontal="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/llHeader"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true">


        <TextView
            android:id="@+id/currentHp"
            android:textStyle="bold"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:text="Current HP: "
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/nextHp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/currentHp"
            android:layout_marginEnd="62dp"
            android:layout_toStartOf="@+id/currentHpValue"
            android:text="Next +10HP: "
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/fullHp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nextHp"
            android:layout_marginEnd="10dp"
            android:layout_toStartOf="@+id/currentHpValue"
            android:text="HP fully recovered: "
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/currentHpValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/currentHp"
            android:layout_toRightOf="@+id/currentHp"
            android:text="a"
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/nextHpValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/currentHp"
            android:layout_toEndOf="@+id/nextHp"
            android:layout_toRightOf="@+id/nextHp"
            android:text="a"
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/fullHpValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/fullHp"
            android:layout_alignBottom="@+id/fullHp"
            android:layout_toEndOf="@+id/fullHp"
            android:layout_toRightOf="@+id/fullHp"
            android:text="a"
            android:textSize="18sp"/>

        <TextView
            android:id="@+id/tvAdditionalInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_below="@id/fullHp"
            android:textSize="18sp"
            android:layout_centerHorizontal="true"
            android:text="Additional info"/>

    </RelativeLayout>

    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rl2"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginHorizontal="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/button"
        android:text=" Ok " />

</RelativeLayout>
Related