87

I am trying to make empty lines within android. This is what I have been doing:

android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="\n\n"

I want to know if there is a better way? Thanks

1
  • 1
    Padding? Margins? It would help to see the whole layout as it is an issue specific to individual layouts. Commented Jun 15, 2011 at 2:08

14 Answers 14

183

Use Space or View to add a specific amount of space. For 30 vertical density pixels:

<Space
  android:layout_width="1dp"
  android:layout_height="30dp"/>

If you need a flexible space-filler, use View between items in a LinearLayout:

<View
  android:layout_width="1dp"
  android:layout_height="match_parent"
  android:layout_weight="1"/>

or

<View
  android:layout_width="1dp"
  android:layout_height="0dp"
  android:layout_weight="1"/>

This works for most layouts for API 14 & later, except widgets (use FrameLayout instead).

[Updated 9/2014 to use Space. Hat tip to @Sean]

Sign up to request clarification or add additional context in comments.

4 Comments

fill_parent is deprecated, so I updated to match_parent. Also, 0dip is recommended in many cases over match_parent.
Is there a way to achieve this programmatically? Does it work the same as LinearLayout but replace LinearLayout with View?
Thanks, I used space as it is created for filling extra spaces
For backwards compatible spaces, use <android.support.v4.widget.Space
29

If you don't need the gap to be exactly 2 lines high, you can add an empty view like this:

    <View
        android:layout_width="fill_parent"
        android:layout_height="30dp">
    </View>

3 Comments

@wizurd Widgets can't use View, use FrameLayout in them instead of View.
This won't be exactly two lines high as the line height depends on the text size, obviously.
Yep. That's what I said in my answer.
20

View if you need change background color , Space if not .

that dosent mean you have to change view background .

<View
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:background="@color/YOUR_BACKGROUND">
</View>

or Space

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

2 Comments

Your hint in regards to when to use View and when to use Space solved my problem of not being able to show a background color. Thanks!
Thank you!!! I was wondering about background color for space.
16

An updated Answer: Since API 14, you can use "Space" View, as described in the documentation.

Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.

2 Comments

An example for anyone wondering: <Space android:layout_width="fill_parent" android:layout_height="10dp" />
Class Space extends View, and thus is heavier than a View
5

if you want to give the space between layout .this is the way to use space. if you remove margin it will not appear.use of text inside space to appear is not a good approach. hope that helps.

<Space
        android:layout_width="match_content"
        android:layout_height="wrap_content"
        android:layout_margin="2sp" />

1 Comment

It is very lightweight view, so we must use Space instead of View.
3
<View
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:background="#80000000">
</View>

Comments

3

I strongly disagree with CaspNZ's approach.

First of all, this invisible view will be measured because it is "fill_parent". Android will try to calculate the right width of it. Instead, a small constant number (1dp) is recommended here.

Secondly, View should be replaced by a simpler class Space, a class dedicated to create empty spaces between UI component for fastest speed.

Comments

2

try this

in layout.xml :

<TextView
        android:id="@+id/xxx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/empty_spaces" />

in strings.xml :

<string name="empty_spaces">\t\t</string>

it worked for me

Comments

2

This can by be achieved by using space or view.

Space is lightweight but not much flexible.

View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is more customizable, you can add background, draw shapes like space.

Implementing Space :

(Eg: Space For 20 vertical and 10 horizontal density pixels)

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

Implementing View :

(Eg: View For 20 vertical and 10 horizontal density pixels including a background color)

  <View
       android:layout_width="10dp"
       android:layout_height="20dp"
       android:background="@color/bg_color"/>

Space for string formatting using HTML entity:

&#160; for non-breakable whitespace. &#032; for regular space.

Comments

1

Just add weightSum tag to linearlayout to 1 and for the corresponding view beneath it give layout_weight as .9 it will create a space between the views. You can experiment with the values to get appropriate value for you.

Comments

0

Agree with all the answers......also,

    <TextView android:text=""
              android:layout_width="match_parent"
              android:layout_height="30dp"
              android:layout_weight="2" />

should work :) I am just messing with others as TextView is my favourite (waste of memory though!)

Comments

0

The previous answers didn't work in my case. However, creating an empty item in the menu does.

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
  ...
  <item />
  ...
</menu>

Comments

0

there is a better way to do this just use the code below and change according to what you want the size of the blank area

     <Space
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_column="0"
            android:layout_row="10" />

if you are using with the grid layoout then only use

 android:layout_row="10" />

Comments

0

Below is the simple way to create blank line with line size. Here we can adjust size of the blank line. Try this one.

           <TextView
            android:id="@id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="5dp"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.