ConstraintLayout alignment with offset - android

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.

Related

Divide screen to two halves with a title for each and center a view inside each one that will shrink or expand to fill the space

I'm trying to create this kind of layout in android.
I'm using constraint layout. I want to divide the screen with height y to two halves with y/2 height each.
Each one of them will have a textview as title which takes the whole width and has constant height.
And a circular custom view which I would like to expand or shrink so it fills the whole half so the padding marked as 'auto' will all be the same but dynamic.
And p will be a minimum padding that the view can't be over them.
The aspect ratio should of those circles should not be changed.
Is that even possible doing it in the xml design editor? or do I have to do it programmatically.
Next step will be to arrange those halves to be side by side if it's a landscape view.
OK, I did it, Basically you drag the 4 edges of a each view to center it in the outside element. And set the margins to .
So I dragged title2 to all directions of the screen and it got centered. Then I dragged each of the circular views edges the same way so the upper view I dragged to title1 and the bottom to title2. The bottom view I dragged its top to title 2 and the bottom to the bottom of the screen.
Auto resizing is done by setting layout_width to 0 and layout_height 0.

How to constraint a view to overlap another view in RelativeLayout

I am overlapping an ImageView on top of a View in a RelativeLayout.
something like this:
1
Where the middle white circle(ImageView) is overlapping the white vertical border(View).
But for some reason I am not managing to constraint the circle above the z position of the white border. and if the border moves say to the right side of the screen, I want the circle to move there too. but instead the circle always remains in the center regardless to the position of the border.
How do I this in a RelativeLayout?
T.I.A
You can make like:
AlignLeftOf the border
Then code: circle.translationX( circle_width/2)
(and a bit of justification, it should be good)

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.

use center of view for alignment

I'd like to use the center of a View to align it in another View, let's say, a TextView in a RelativeLayout.
I don't want to have it centered inside the RelativeLayout but rather offset from the bottom left corner.
At the moment I align the TextView ParentLeft and ParentBottom and use margins on left and bottom to move it away from those sides.
However, because my TextView is set to wrap_content in width, it "moves" around when the text inside and with it its width changes (since the borders of the TextView are used for the offsets).
I want the center of the TextView to stay on my offsetted position.
Is this possible in the xml, without having to write code that manages the offset or to use another layout?
That's the unwanted behaviour when the text changes: Screen1
The desirable behaviour: Screen2
Note that the center of the TextView stays the same.
There is a simple logic here. If you want to align a view with respect to the parent then for relative layout you can use align_parent wherein in linear use android:layout_gravity. Here in you case make textview as match_parent and set gravity to centre.

Set negative margin on ImageView shifts ImageView but not Image

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.

Categories

Resources