At the moment I'm playing a bit with the new ConstraintLayout and try to create the following layout:
(1) Headline: Type 24sp, Leading 32sp
(2) Subhead 1: Type 15sp, Leading 24sp
(3) 32sp line height
(4) 56dp padding between the top of button and the center of copy
(5) 24dp vertical padding
My problem now is that I do not know how I should handle the sizes which refer to the center of the second TextView. We do not have a constraint for the center (or baseline) to bottom or something like this. Also I'm not able to set a space between two views which are connected between the baseline points. So how should I create this layout with the ConstraintLayout?
Can someone please give me a sample layout from the picture above?
We only allow baseline to baseline, without margin for now. We might revisit this in the future, but as of ConstraintLayout alpha 7 you cannot do this. The only option is to align with top / bottom.
You could also add a view with a layout height as a divider, then reference the bottom of the divider
I have a very small question which occurred while reading wrap content and parent content that what is plus padding and minus padding?
According to me i think plus padding means increase distance (from margin) x-axis and decrease x-axis . is it so?
Related answer: Difference between a View's Padding and Margin
Padding can be seen as an internal margin, or a margin applied to elements inside the padded element, as seen in f.ex. a TextView, where a padding would shift the text away from the border, or make the TextView larger to accomodate the padding. In a layout, using padding will shift all the content away from the padded edges. Using negative padding will shift the content towards the edges, so using android:paddingLeft = "-5px" would shift the content to the left
Yes you are right.
For example ->
android:paddingLeft = "4dp" means it will take shift the text to right by 4dp.
android:paddingLeft = "-4dp" means it will take shift the text to left by 4dp.
This question already has answers here:
Difference between a View's Padding and Margin
(15 answers)
Closed 4 years ago.
I have referred questions on SO. ALso checked an answer:
Padding is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent).
Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object. Note that, like the padding, the margin goes completely around the content: there are margins on the top, bottom, right, and left sides.
Also, more on padding and margins from:
http://developer.android.com/reference/android/view/View.html
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html
But what is the difference fundamentally between padding and margins ? Would the behaviour differ depending on O.S. and devices?
I have a normal, simple layout. No problem with code, have used layout folders - layout and layout-sw600dp plus drawables-4dpi. Can't make layout without margin or padding, which one is more appropriate?
Margin
Margins make up the vertical and horizontal areas between elements. If elements have no margins around them, they will bump right up against each other. In other words, he space outside of, or between, elements is what comprises the margin areas.
Padding
The padding of an element is the horizontal and vertical space that’s set around the content area of the targeted element. So padding is on the inside of a box, not the outside.
Padding is for inside/within components. Eg. TextView , Button, EditText etc.
Eg. space between the Text and Border
Margin is to be applied for the on-outside of the components.
Eg. space between left edge of the screen and border of your component
Visual representation is great in : Difference between a View's Padding and Margin
With Padding, i have seen a difference in 2.2, 2.3 and say 4.3, 4.4
in such cases:
<RelativeLayout 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: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" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="ASDFGHJKL" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:text="#string/hello_world" />
</RelativeLayout>
Also, check the use of dimens:
http://developer.android.com/guide/topics/resources/more-resources.html
In Simple words .. if you want to take your widget like TextView, EditText far away from other. You should use margin from top,right,left,bottom.
By increasing padding it will increase the inner spacing not making the widget far apart from others..
Like buttons, for example, the characteristic button background image includes the padding, but not the margin. In other words, adding more padding makes the button look visually bigger, while adding more margin just makes the gap between the button and the next control wider.
Margin: Between border and its parent layout
Padding: Between content and border
refer to this
The difference between margin and padding and use cases are clearly explained by +Nick Butcher in Udacity's video. Here's the excerpt:
...if you want the touchable area or the background of the object to be enlarged, then use padding, otherwise use margin...
what is the difference fundamentally between padding and margins ?
For the differences - Rohan Khandwal has shared a very perfect link.
Would the behaviour differ depending on O.S. and devices?
Now If we are talking about the behaviour of the view which has been given diffrent margins & padding. Then It will definitely look diffrent in different devices with diffrent resolutions.
Thats why we are given diffrent dimen/values/layout folders which have their own meanings.
The difference between android margin and padding is that even though the text is how much sp you want away from the edge, margin is not spaced or colored in. It is only the text and the color you set with it all "alone". With padding though, the text is away from the edge of the screen just like margin but, in padding the text is away and all the space between the text and the edge of the screen is filled in with the color or any text preference you chose to be. This is the difference between android margin and android padding.
Padding is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent).
Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object. Note that, like the padding, the margin goes completely around the content: there are margins on the top, bottom, right, and left sides.
Padding Increases the size of the view where as margin doesn't because it is outside the view
The padding is expressed in pixels for the left, top, right, and bottom parts of the view. Padding can be used to offset the content of the view by a specific amount of pixels.
For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.
Padding can be set using the setPadding(int, int, int, int) method and queried by calling getPaddingLeft(), getPaddingTop(), getPaddingRight(), and getPaddingBottom().
Margins are the spaces outside the border, between the border and the other elements next to this view.
Note that, like the padding, the margin goes completely around the content :there are margins on the top, bottom, right, and left sides.
Margin can be set using the setMargins(int left, int top, int right, int bottom) method.
I'm trying to have some margins inside a button between the text and the borders but I don't know how to specify them.
The only way I know is using android:layout_width="...", but this is not relative to the text size.
Thanks!
If you're looking for this kind of padding on the left and right of this cheeky text :
Here is what you do. :
<Button
android:id="#+id/she_was_good"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:soundEffectsEnabled="false"
android:layout_alignParentBottom="true"
android:paddingRight="25dp"
android:paddingLeft="25dp"
android:text="#string/im_sorry_lol_str" />
The part you are looking for is paddingRight and paddingLeft
Use padding to add margin between border and text in button.
To measure its dimensions, a view takes into account its padding. The
padding is expressed in pixels for the left, top, right and bottom
parts of the view. Padding can be used to offset the content of the
view by a specific amount of pixels. For instance, a left padding of 2
will push the view's content by 2 pixels to the right of the left
edge.
Read more at the official doc.
I am trying to design a screen and unable to appreciate the difference between android:layout_gravity and android:padding, if both align the view based on parent layout, then how different are they from each other. Please advise. Thanks.
Padding (see the "Size, Padding and Margins" heading in that link) creates an absolute distance (usually in pixels) between the edge of a view and it's content.
Gravity positions an item relative to it's parent: in the center, to the left, to the right, top, bottom, etc. but you cannot supply an absolute distance from an edge, e.g. 10 pixels, without using margin (or padding; read the Android docs for information on the difference between those two).
I like to think of padding as a box inside the layout where your contents will be placed (alignment baed on the gravity). Where as, without any padding, gravity will change the alignment of the contents with respect to the layout border.
android:padding is setting the absolute padding values within the current layout. android:layout_gravity set the gravity of the current layout with respect to the parent.
<Linearlayout android:id="#+id/L1">
<Linearlayout android:id="#+id/L2"
...
android:layout_gravity="bottom|left"
android:padding="5px">
<TextView android:id="#+id/T1"
.../>
</Linearlayout>
Hence L2 is aligned to the left bottom corner of L1 and android:padding sets a padding of 5px on each of the four sides for the layout L2 (which affects the childlayouts of L2 = T1).