9

I am new to android development and I was just wondering how can I add space between two TextViews? Any help would be appreciated.

The code I have written so far

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lbl_group_coworkers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Coworkers" />

    <TextView 
        android:id="@id/lbl_group_"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Family"/>



</LinearLayout>
Alvaro Gutierrez Perez
  • 3,669
  • 1
  • 16
  • 24
Anmol Wadhwa
  • 329
  • 2
  • 3
  • 8

11 Answers11

18

You can use android:layout_marginTop="value" like this

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/lbl_group_coworkers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Coworkers" />

        <TextView 
            android:id="@id/lbl_group_"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Family"/>
    </LinearLayout>
Ali Ahmad
  • 1,351
  • 9
  • 11
6

you can have margin between two textview. add top margin to the second textview

like this

 <TextView 
    android:id="@id/lbl_group_"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:text="Family"/>
rachit
  • 1,981
  • 15
  • 23
3

Just replace <LinearLayout> </LinearLayout> with <RelativeLayout> </RelativeLayout> then go in graphical layout and adjust space as you like.

John R
  • 2,078
  • 8
  • 35
  • 58
1

add margin left right top bottom

 android:layout_marginLeft="10dp"
yuva ツ
  • 3,707
  • 9
  • 50
  • 78
1

add android:layout_marginRight="..." to the first textView

Deepzz
  • 4,573
  • 1
  • 28
  • 52
Evgeni Roitburg
  • 1,958
  • 21
  • 33
1

try adding margin

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/lbl_group_coworkers"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Coworkers" 
    android:layout_margin="10dp"/>

<TextView 

    android:id="@+id/lbl_group"
    android:layout_margin="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Family"/>

</LinearLayout>
Anukool
  • 5,301
  • 8
  • 29
  • 41
1

You can use

android:layout_margin="5dp"

or

android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"

But before you ask a lot more such questions, I'd suggest to read through the android dev guides ( http://developer.android.com/guide/components/fundamentals.html )

Good Luck and have fun developing...

jpm
  • 3,300
  • 1
  • 19
  • 29
1

You can use either padding or margin depending on what you need, here is a link of a guy who gives a good explanation between the two that will help you decide on which one you'd like to use: android margin vs padding

Community
  • 1
  • 1
cking24343
  • 3,173
  • 1
  • 21
  • 23
1

set margin property in TextView...

android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"

and if all side set space then...

android:layout_margin="20dp"
0

Use GridLayout instead of LinearLayout

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A"
            android:layout_gravity="left" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="B"
            android:layout_gravity="right" />
</GridLayout>
-1
  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/custom_border"
  >
  <TextView
      android:text="@string/textView_reference_number"
      style="@style/CustomTextView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/textView_refernce_number_label"
      />
   <TextView
      android:text="Reference Number Value"
      style="@style/CustomTextView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/textView_refernce_number"
      android:layout_marginTop="10dp"/>   
 </LinearLayout>

android:layout_marginTop XML attribute is used to specify extra space on the top side of this view. Thus android:layout_marginTop="10dp" on 2nd textView is specifying 10 dp of space between it and above view. @UDI Please refer below link for more details over the same https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html

SFDCCoder
  • 23
  • 4
  • Can you explain what are doing in your answer. Simply pasting code won't help any one to understand the concept. – UDID Nov 23 '16 at 12:17
  • android:layout_marginTop XML attribute is used to specify extra space on the top side of this view. Thus android:layout_marginTop="10dp" on 2nd textView is specifying 10 dp of space between it and above view. @UDI Please refer below link for more details over the same. https://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html – SFDCCoder Nov 27 '16 at 05:55
  • Please check it. Hope it helps. Please comment if you require any other help or have any other confusion. @UDID – SFDCCoder Nov 27 '16 at 06:01
  • Please add this in to answer this will help for future use. – UDID Nov 28 '16 at 05:12