prepbgg
prepbgg

Reputation: 3652

TextView ignores left and right margins

My layout includes:

<?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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnFindPOIs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="Find POIs"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnGetLocation" />

    <TextView
        android:id="@+id/txtPoiDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnFindPOIs" />

</androidx.constraintlayout.widget.ConstraintLayout>

When the content of TextView extends beyond one line it correctly flows to a second line but the text extends right to the edge of the screen ignoring the 16dp left and right margins.

Upvotes: 0

Views: 1566

Answers (1)

River_B
River_B

Reputation: 85

Layout Margin affects the View object, in this case a TextView. Not the actual text inside the object. So it will still go right up against the object's edge. I would recommend using padding, it will directly affect the text. You may also want to consider adding gravity to the object?

I would also recommend using layout weights on these views or you are going to have resizing issues!

android:paddingStart="16dp"

Cheers!

Upvotes: 1

Related Questions