How to Give Space Between Two TextView in Android

Some people don’t know how to give space between TextView in Android Studio. Two TextView if not given space will look too tight. Therefore, give a space between the two. To provide space between two elements, there are several ways. That element can be TextView with TextView, TextView with buttons, or buttons with other buttons.

In this tutorial article, I will tell you how to provide space between elements in two ways:

  1. Space
  2. Layout_Margin

You can give background to your Android application. How do you give a background image? You can read this article android studio background image. If you want to give a gradient background, read this article android studio gradient background.

Step to Give Space Between Two TextView in Android

For your information, I use Android Studio version 3.6.1

  1. Create a new project.
  2. Select a project template click Empty Activity then click Next.
  3. Configure your project, type project name. For example, in this tutorial I give the project name: Space Between TextView, then click Finish.

This is the activity_main.xml at the first time:

<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  1. Change the layout to LinearLayout from ConstraintLayout. At android:orientation=”vertical”. The code like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
  1. Add TextView element to second row. The code like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="20sp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="ROW 2"
        android:textSize="20sp"
        />

</LinearLayout>

The result would look like this:

Space Between Two TextView
Space Between Two TextView

Add Space in Android – With Space

  1. Add the space element between the TextView. See the code below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="20sp"
        />

    <Space
        android:layout_width="match_parent"
        android:layout_height="20dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ROW 2"
        android:textSize="20sp"
        />

</LinearLayout>

The result would look like this:

Add Space in Android – With Layout_Margin

  1. Add android:layout_marginTop=”20sp” to the code. See the code below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:textSize="20sp"
        />

       <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ROW 2"
        android:textSize="20sp"
        android:layout_marginTop="20sp"
        />

</LinearLayout>

The result would look like this:

Thus the tutorial on how to provide space between TextView in Android Studio. I hope this tutorial helps many people all over the world. Don’t forget to share with others if you helped by this tutorial.

Thank you very much.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *