Using Android views, I've stumbled upon a recurring issue and thought it was about time to ask if anyone has a solution.
I have a parent ConstraintLayout, the blue one in the picture.
This layout contains two views: a green view constrained to the left of the parent, and the red view constrained to the center of the parent.
Both red view and green view are text views, and their actual size may vary depending on the language.
My wish is for the red view to always stay centered, so to grow symmetrically left and right until it reaches the green view. At its maximum width, the red view will touch the green view on the left, and there will be empty space on the right of the same width as the width of the green view.
Problem is that I can't find a way using regular XML layouts to do it. I can think of several hacks to do it, but thinking there should be a clean way.
Any idea?
Not sure but just an idea, maybe you can try to add two more barriers. One at the end of the green box at left. And one for it's symmetric. Because you said
and there will be empty space on the right of the same width as the
width of the green view
So with these two barriers you can mark the borders of the red one.
And you can set the constraints of the red to the barriers, and with 0dp width may work. Let us know :)
If you are certain that the width of the red view will not ever need to go to two lines because it runs out of space (maybe it is truncated, marque'd or ellipsised) then you would simply constrain the start and end of the red view to a guideline set in the center of the ConstraintLayout.
However, if you can't guarantee that the red view will never need two lines then you are stuck with a hack. The simplest hack would be to create an invisible view on the right that has the same width as the green view. (It could simply be another TextView with the same text and characteristics.) You would then constrain the start of the red view to the end of the green view and the end to the start of the invisible view.
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 am changing my buttons' backgroundDrawable programaticaly:
myButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.card_button_blue));
After changing the background, the text in the button is not aligned any more like it was before changing the background.
(source: dsliga.eu)
target SDK version is 10.
Thanks.
Looks like the setText() method messes up the alignment. After changing the text, re-applying the gravity and padding did the job:
myButton.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
myButton.setPadding(12, 12, 12, 12);
Since you are using a 9patch, you need to specify with bottom and right side lines where the data is inside your drawing, you probably did not draw a full line at the bottom, and right side of the 9patch drawing
this is why the text inside your button stopped being centered, because the bounds of the 9patch are NOT what you think they are
your 9patch should (normally), leave only 1 pixel at the top and bottom of the right side line, and 1 pixel at the left and right of the bottom line
redraw the 9patch, remove the gravity and padding code, and see if it works
see below image
I created a custom drawable for the background of an EditText. I need the text to be left and bottom aligned. It renders fine on screen but the text is not aligned to the right nor the bottom of the drawable. I also tried gravity:bottom and tried setting the padding on top in hopes it would move it down. Is there anything else I can do?
The right and bottom lines define the content area.
The line you draw on the right spans the top half of your image. That's why the text won't align to the bottom.
You should draw a slightly longer line.
Check out the image used by default. (I changed it to blue here though).
This will give the same result you are having. The text won't be aligned to the bottom.
Now if I extend the right line to the bottom, I would obtain the below image.
This will give you the expected result.
I Need to shift an ImageView, which is a direct child of a LinearLayout, a bit to the left.
What happens: The View is shifted (can see the "bounding box" is shifted, with android layout manager) but the drawable keeps in the same place.
Also the drawable is cut on the right side, if I set margin low enough.
Why is this? Any advice?
Clarification: I have to shift the Image to the left. That means a bit ouside of the bounds of the containing layout.
It seems elements are always clipped when they get outside of parent's bounds (also with clipChildren=false).
A solution is to create an additional container-layout for everything besides of the item to be shifted. And then shift the container-layout in the opposite direction.
For example, to shift -10dip:
<Original container ...>
<Item to be shifted/>
<New container with margin 10dip>
<Previous content of Original container ... />
</New container with margin 10dip>
</Original container>
If your container have enough padding you can set its clipToPadding to false and use negative margin!
Why are you using negative margin? use a positive a bit more to the opposite side of the ImageView to the direction you want to shift the image, i.e. if you want to shift the image to the left, use more positive margin to the right.