How to remove space between the text and view after using weights in linear layout?

Viewed 22

My objective is to create a layout where there is a line , a text immediately after that and a line for the remaining space horizontally. I am trying to do it with view> textview>view with weights , but the textview doesn't seem to cover the space at start and end. So how to remove that space ? Here is my code

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:orientation="horizontal"
        android:layout_weight="0.01"

        android:gravity="center"
        android:layout_height="wrap_content">
        <View
            android:layout_width="0dp"
            android:layout_weight="0.2"
            android:layout_height="2dp"
            android:background="#000000"/>
        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="0dp"
            android:text="DELIVERED"
            android:textSize="16sp"
            android:gravity="center"
            android:includeFontPadding="false"
            android:textColor="@color/black"
            android:fontFamily="@font/roboto_regular"
            android:layout_weight="0.3"
            android:layout_height="wrap_content"/>
        <View
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="2dp"
            android:background="#000000"/>
    </LinearLayout>

As you can see there is space before and after delivered.

Thanks :)

2 Answers

I removed the layout weight from the text and made its width to wrap content, and now the lines touch the text as you like, not sure if that's what you want but now the UI as I Understood from your request

<LinearLayout
        android:layout_width="match_parent"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:orientation="horizontal"
        android:layout_weight="0.01"

        android:gravity="center"
        android:layout_height="wrap_content">
        <View
            android:layout_width="0dp"
            android:layout_weight="0.2"
            android:layout_height="2dp"
            android:background="#000000"/>

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:gravity="center"
            android:includeFontPadding="false"
            android:text="DELIVERED"
            android:fontFamily="@font/roboto_regular"
            android:textColor="@color/black"
            android:textSize="16sp" />
        <View
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="2dp"
            android:background="#000000"/>
    </LinearLayout>

try the below code with ConstaraintLayout

 <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.01"
    android:gravity="center"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="#000000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/appCompatTextView"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/appCompatTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:fontFamily="@font/roboto_regular"
        android:gravity="center"
        android:includeFontPadding="false"
        android:text="DELIVERED"
        android:textColor="@color/black"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="#000000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/appCompatTextView"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

Related