0

In iOS I can just set a space in between items, how can I achieve that in Android?

For example:

I want to have a TextView on the left of the screen, pinned on the left edge of the parent and a TextView on the right of the screen, pinned on the right edge of the parent, both in the same line.

Now I want the left TextView to have a width as wide as possible, but minimum 16dp space between the left TextView and the right TextView.

I hope it is understandable.

Micho
  • 3,929
  • 13
  • 37
  • 40
Bioaim
  • 928
  • 1
  • 13
  • 26
  • If I understood what you want you can just use `padding` to add the space you need. And if you use a `RelativeLayout` as parent layout, you can align to the right and left the `TextView` – Angelo Parente Mar 09 '17 at 22:38

2 Answers2

7

In terms of leaving space around a view, you'd be looking at using a margin (see here for the difference between padding and margin in Android layouts).

Specifically in ConstraintLayout it would look something like:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/left_text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/right_text_view"
        android:layout_marginRight="16dp"
        android:text="left text" />

    <TextView
        android:id="@+id/right_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toRightOf="parent"
        android:text="right text" />

</android.support.constraint.ConstraintLayout>

Note the differences between the widths of the TextViews. The right one is wrap_content so it will vary it's size to fit it's text. The left one has width of 0dp, which in a ConstraintLayout means "match constraints". It needs a left and right constraint in order to work out what width to be, so we've given both.

We've also added a margin to the right of the first TextView to ensure a gap between views. Alternatively we could have added the 16dp as layout_marginLeft on the second TextView or split it 8dp each, depending on what makes most sense in the circumstances.

Community
  • 1
  • 1
Lewis McGeary
  • 7,692
  • 2
  • 40
  • 46
1

If I understood what you want you can just use padding to add the space you need at the TextView. And if you use a RelativeLayout as parent layout, you can align to the right and left the TextView

Angelo Parente
  • 796
  • 2
  • 6
  • 15