Set negative margin on ImageView shifts ImageView but not Image - android

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.

Related

Center a view up to a barrier

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.

ConstraintLayout alignment with offset

I am using a ConstraintLayout with two views inside. An ImageView that varies in size depending on the image loaded and a View which acts as a custom made dropshadow for the Image. What I've been doing so far is adding 8dp padding to the ImageView and then aligning all of the edges of the View to the ImageViews, thereby making the View stick out 8dp underneath the ImageView.
Now for several reasons I don't want to keep the padding on the ImageView but I still want to achieve the same effect. So basically I would like to align the Left of the View to 8dp left of the Left of the ImageView (and the same for every other edge, right, top, bottom).
Is there any way to achieve this?
Thanks in advance!
I´ve been having problems with this too, it seems ConstraintLayout does not support negative margins yet: https://code.google.com/p/android/issues/detail?id=235306.
In my case, I want a 20dp negative margin to a set of overlapping images on a header image. So far, I'm positioning an empty TextView below the guideline my header image is connected to, with a 20dp top margin and aligning my overlapping image views to that.

relativelayout space between its elements when background image is present

I have a relative layout with background image (9patch). I have set this image as background to the layout using xml. When I add another relative layout which has buttons and textviews, there is little space left before the new layout. I didnt set any padding. I also tried by setting padding top to 0. If i remove the background image the alignment will be proper with no space left on top of new layout. I have also tried by setting match parent and fill parent in new layout but still the space remains on top of it. The space left is very little like 3dp-5dp.
can someone suggest about this issue pls.
EDIT : space is on top of the new layout horizontally and i have tried setting gravity to top and also top left.
The black pixels on the top and left define the stretch area of the 9-patch, while the pixels on the bottom and right define the fill area. My guess is that you have a fill area defined that does not cover the entire right and bottom edges.
Here's a good guide.

Android padding problem

:) I'm having the following problem: I have a view and i want to add borders to it. What I'm currently trying to do is to set padding to the view (padding from all the sides) and set background color to it which will fill the padding. The thing is that it seems to me that it's possible to set padding either only from top and left or from bottom and right but not from all of them together. I.e if i write
view.setPadding(border,border,border,border)
this will set padding only from top and left. In order to set padding from bottom and right I have to write:
view.setPadding(-border,-border,0,0)
which won't leave left and top padding and so on. If I try to use margin it moves the whole block(the view + the padding area), but not only the view, so this doesn't seem to work either. Any ideas on how to do it without having to use a wrapping layout? Thanks!
What exactly happens when you use the first example?
The four int parameters for setPadding() are for left, top, right, and bottom, respectively. So, calling setPadding(4, 5, 6, 7) should give you 4 pixels of space for the left edge, 5 for the top, 6 for the right, and 7 for the bottom. What result are you getting when you do this? Can you show a screenshot?
What is the content of your view? If it's an image or something similar, perhaps it's not being centered or scaled properly. Try calling setGravity(CENTER);.

What is android:layout_gravity="clip_vertical" exactly

The property android:layout_gravity="clip_vertical|horizontal" does the following as mentioned in the SDK documentation:
Additional option that can be set to
have the top and/or bottom edges of
the child clipped to its container's
bounds. The clip will be based on the
vertical gravity: a top gravity will
clip the bottom edge, a bottom gravity
will clip the top edge, and neither
will clip both edges.
But I can't see anything of this in my applications,
so what is the purpose of this property exactly ?
thanks
Short version:
clip_horizontal and clip_vertical apply to the measurements of the view itself, before any contents (such as the image in a BitmapDrawable) are rendered.
Long version:
I've run into some similar confusion over clip_horizontal and clip_vertical. (In my case, it was related to android:gravity for a BitmapDrawable, but it's similar enough to be applicable.)
From the documentation I thought that something like android:gravity="top|left|clip_vertical" on a bitmap would cause the image's top left corner to be positioned at the view's top left corner, and that, if the bitmap was taller than the view, it would be "clipped" at the bottom edge of the view. In other words, show only as much of the bitmap that the view is tall enough to reveal; do not stretch the bitmap, but instead only show whatever will fit, letting the rest extend below the bottom edge.
However, the opposite happened: when I set clip_vertical, a large bitmap was squished vertically to fit within the height of the view.
After examining the applyDisplay() method in platform/frameworks/core/java/android/view/Gravity.java, I realized my mistake:
It isn't the bitmap image that was going to be clipped, but the view -- the actual size of the container the image is ultimately rendered into.
Setting clip_vertical in my case didn't mean "clip the image at the bottom edge," it meant "clip the BitmapDrawable's view itself so its height matches the height of its parent container"...which then caused the image to be "squished" as it filled that shorter height.
So, the important thing to remember with android:gravity and android:layout_gravity is that clip_horizontal and clip_vertical apply to the measurements of the view itself, before any contents (such as my BitmapDrawable) are rendered.
Maybe there is no effect because horizontal is not defined in the android:layout_gravity. clip_vertical is just an additional property that is used in addition to a base property.
Flag to clip the edges of the object to its container along the horizontal axis.
check this

Categories

Resources