How to Make Space Between Linearlayout Children

How to make space between LinearLayout children?

You should android:layout_margin<Side> on the children. Padding is internal.

Best practices for spacing between elements in LinearLayout

Try 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="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />

<android.support.v4.widget.Space
android:layout_width="8dp"
android:layout_height="wrap_content" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />

<android.support.v4.widget.Space
android:layout_width="8dp"
android:layout_height="wrap_content" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />
</LinearLayout>

Add space to your code .

<android.support.v4.widget.Space
android:layout_width="8dp"
android:layout_height="wrap_content" />

Horizontal LinearLayout space between items without padding left and right

Edit: better solution

Setting layoutWeight on edge views would remove padding around them.

<LinearLayout
android:id="@+id/requested"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_marginBottom="32dp"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@id/original">

<ImageView
android:id="@+id/rateOneStar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_weight="0"
app:srcCompat="@drawable/ic_round_credit_card_24"
app:tint="@color/colorLightBlack" />

<ImageView
android:id="@+id/rateTwoStar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_weight="1"
app:srcCompat="@drawable/ic_round_credit_card_24"
app:tint="@color/colorLightBlack" />

<ImageView
android:id="@+id/rateThreeStar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_weight="0"
app:srcCompat="@drawable/ic_round_credit_card_24"
app:tint="@color/colorLightBlack" />

<ImageView
android:id="@+id/rateFourStar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_weight="1"
app:srcCompat="@drawable/ic_round_credit_card_24"
app:tint="@color/colorLightBlack" />

<ImageView
android:id="@+id/rateFiveStar"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_weight="0"
app:srcCompat="@drawable/ic_round_credit_card_24"
app:tint="@color/colorLightBlack" />
</LinearLayout>

Result

how to increase the space between the elements in linearlayout in android?

You should set layout margin like below, in your edittext in .xml file

 android:layout_marginLeft="5dp"

you can do that for marginTop Bottom, Right and left , depends on your requirement. You must specify this on the layout elements, not on the layout itself.

difference between padding and Margin:-
Padding related to space inside view, and Margin is the space outside view or space between two views,

if space required outside view in any one direction, can be achieved as given above. and if space required on all side of view surroundings below tag can be useful

android:layout_margin="5dp"

How to specify spacing between elements of LinearLayout only once?

the way that is recommended is to apply a style to all the elements in the linear layout

android:style="@style/mystyle"

<style name="mystyle">
<item name="android:layout_marginTop">10dp</item>
... other things that your elements have in common
</style>

How to make children in a LinearLayout occupy the same ammount of space

I think the problem is that, in addition to the other answers, the TextViews have width="wrap_content". Try this instead:

<LinearLayout
android:id="@+id/line_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/line_2"
android:layout_marginBottom="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:baselineAligned="false"
android:gravity="center_vertical" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:background="#FFFFFF"
android:gravity="center_horizontal">

<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/sights_selector" />

<TextView
android:id="@+id/corfu_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sights"
android:textSize="10sp" />

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:background="#FFFFFF"
android:gravity="center_horizontal">

<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/activities_selector" />

<TextView
android:id="@+id/epirus_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activities"
android:textSize="10sp" />

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_weight="1"
android:background="#FFFFFF"
android:orientation="vertical">

<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/accomodation_selector" />

<TextView
android:id="@+id/vlora_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Accomodation"
android:textSize="10sp" />

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_weight="1"
android:background="#FFFFFF"
android:orientation="vertical">

<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/villages_selector" />

<TextView
android:id="@+id/vlora_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Villages"
android:textSize="10sp" />

</LinearLayout>

</LinearLayout>

How to give Space between Linear Layouts in android

You don not need to wrap you text-views into linear layouts.

I would make the top layout linear too and a second for data1 and data2

Using layout_weights and the space-view can now give you a nice and easy modifyable result.

Like that:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">

<TextView
android:id="@+id/text1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="45"
android:background="#77ff3f"
android:text="data1" />

<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="10" />

<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="45"
android:background="#ff200f"
android:text="data2" />

</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">

<TextView
android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2c23ff"
android:text="data3"></TextView>

</LinearLayout>

</LinearLayout>

EDIT:
If you use weight always set the weighted length to 0dp.



Related Topics



Leave a reply



Submit