I am trying to implement a line with selectors at each end that a user can then click on and drag to stretch the line.
The way I've approached this is to draw a line inside a view, draw a selector inside a view, and then add the line and selector views to a ViewGroup.
Problem: If I try to change the length of the ViewGroup container, it will not re-size beyond the width of the screen. If I try to stretch the ViewGroup container, then sizing is OK, but it stretches the selectors as well.
Drawing the selectors outside of the ViewGroup will become quite messy fast, especially during shape rotations.
Any suggestions?
Related
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.
I want to create a custom view which should have animation. I want to draw the circle with specifying the radius and some drawable element in this custom view which should be moving in this circle.
I know how to draw a circle, how to add a drawable element, but I don`t know how to animate this drawable element.
Should I write animation methods in custom view or better to create another class with animation logics and apply it to my custom view. Can you give me some advice?
I think that second case is wrong because in this way a whole view will be animating, but not only drawable.
I have a custom ViewGroup that contains an ImageView and a few other TextViews that display white text on the image. I would like to draw ONE dark rectangle under all the textviews to improve the contrast.
To accomplish this, in the custom ViewGroup's onLayout, I compute the smallest rectangle that contain all of the TextViews. Now, I have no idea how to draw the rectangle on top of the first child (the image), but below the other children, the TextViews. So I would like to draw the children in the order:
ImageView (first child)
Computed dark Rect
TextViews (all other children)
I tried overriding dispatchDraw() and then after the call to super.dispatchDraw(canvas), I drew the rectangle. But that obviously put the rectangle over the TextViews.
I would appreciate any help.
Thanks!
I have a custom view that I'm using canvas to be able to draw on it what I want, this view inside a scrollview that take match_parent for each width and height and also my custom view take match_parent for each width and height
I want to know the size of the scrollview to be able to draw only inside the available space that I have
I have tried onLayout, onMeasure but nothing works with me
Can any one please help?
I have solved this problem using
onGlobalLayout Method, and I also inherit Viewgroup not View and implement onLayoutMethod, to make element shown in my fragement
Thanks all
Right now I have a view that I'm just popping onto the screen by changing the view from GONE to VISIBLE, and I instead want to have a TranslateAnimation that shifts the view onto the screen. As soon as the animation starts the view on the right becomes clipped though. I assume this is because the parent view is taking the width of the view on the left and factoring that into the view calculation? Is there any way to not have that happen, so it looks like both views are shifting onto and off of the screen? Setting
android:clipChildren="false" android:clipToPadding="false"
didn't seem to help
Try to set android:clipChildren="false" android:clipToPadding="false"
on the parent container