Search code examples
androidtextview

The Text touches the start of Parent when there is more than one line of output in TextView


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rotationX="0"
    tools:context=".MainActivity">

<EditText
    android:id="@+id/editText"
    android:layout_width="291dp"
    android:layout_height="67dp"
    android:layout_marginTop="32dp"
    android:ems="10"
    android:inputType="numberDecimal"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="0.012" />


<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="396dp"
    android:text="@string/submit"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    android:onClick="claculate"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#00FA9A"
    android:textSize="18sp"
    app:layout_constraintBottom_toTopOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText"
    app:layout_constraintVertical_bias="0.509" />
</androidx.constraintlayout.widget.ConstraintLayout>`

When there is more than one line of output in @+id/textView2 the text touches the left/start of the parent layout.How can I make it not to touch the start of the parent view when there is more than one line of output.

Here is the calculate function :

public void claculate(View view){
        String s = editText.getText().toString();// Taking the value of the editText and converting it to string Using to string.
        Double kg = Double.parseDouble(s);// converting string to int.
        double pound = 2.205 * kg;
        textView2.setText(MessageFormat.format("The value in the pounds is {0}lbs", pound));
    }
}

Thanks in advance:)

Solution

  • Try using android:gravity = "center". When the output is long enough to touch the both end of the parent it will center the text and avoid aligning to one side.