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?
-
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 Answers
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:

- 11,034
- 2
- 27
- 28
-
-
nice way to understand margin and padding example for fresher truly saying!!! – duggu May 03 '13 at 08:14
-
A good picture, but I would also remove a black border around content and the outest one black border. – Zon Jul 26 '17 at 12:41
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"

- 20,936
- 12
- 75
- 93

- 27,299
- 12
- 60
- 71
-
1
-
2one more thing, what you asked was MARGIN, padding is some space of text in the edittext itself. like if you say padding_left in edit text, it will start some space before writing text in it.. if my answer helped pleas accept it. – AAnkit Jan 17 '12 at 09:28
-
It's a bad practice to add boiler plate code. You should either use a theme or a bland drawable as a divider – Alex.F Jul 29 '14 at 15:06
-
-
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"

- 6,944
- 6
- 40
- 75
-
-
1
-
@YoushaAleayoub dp is a format to give distance or size to views in android and 10 is just a number like 10px where px is for pixel – SilentKiller Sep 22 '16 at 09:15
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);

- 6,161
- 3
- 20
- 14