31

In my application, I had Used linearlayout, inside that I am having 3 EditText elements. Now I want to increase the space(padding) between the Edittext element. Is it possible?

Thiru
  • 2,374
  • 7
  • 21
  • 29
  • Just to point out, the correct answer is not the accepted from the "In Android, how to..." but one [that proposes to use divider](http://stackoverflow.com/a/18538656/923340) as this gives you one place to apply space to any number of children. – Lukasz 'Severiaan' Grela Oct 14 '14 at 08:57
  • I've added an [additional answer](https://stackoverflow.com/questions/4259467/in-android-how-to-make-space-between-linearlayout-children/56907156#56907156) to the linked answer, and it simply uses ```.setPadding()``` on the children of the LinearLayout. – ConcernedHobbit Jul 05 '19 at 17:32

4 Answers4

47

Basically this is relative to your border, if you want to add the space between the elements inside the border (i.e. have the borders touching), you should use the padding property.

If you want to add the space outside the borders (have the borders apart) you should use the margin property.

Check this picture for illustration:

enter image description here

MahdeTo
  • 11,034
  • 2
  • 27
  • 28
45

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"

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
AAnkit
  • 27,299
  • 12
  • 60
  • 71
13

To give Margin between fields can assign in four directions.

android:layout_margin="10dp"

you can also use followings for different directions..

android:layout_marginLeft = "10dp"

android:layout_marginRight = "10dp"

android:layout_marginTop = "10dp"

android:layout_marginBottom = "10dp"

SilentKiller
  • 6,944
  • 6
  • 40
  • 75
6

Just to add you can change it dynamically too

int left = 6;
int top = 12;
int right = 6;
int bottom = 6;

TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);

EditText edXY = new EditText(inventory.this);
edXY.setLayoutParams(params);
Shadow
  • 6,161
  • 3
  • 20
  • 14