I have been working with Android for about 4 months. I need some guidance on how to implement something like this:
In that picture I've used three of the custom viewGroups (calling it ArcScrollView) stacked on top of on another which is what I am trying to achieve.
The questions that I have are:
Do I need to extend ViewGroup, frameLayout or what?
How can I make it scrollable?
I took a look at LinearLayout source code and it was extending frameLayout but again LinearLayout is not scrollable and need to be hosted by a scrollView.
I need to have complete control over scrolling because these arcScrollView are rectangles and I need onTouchIntercept of the viewGroup to return false (not consuming) at certain positions.
Is scrolling actually just changing the starting position of the first child view and putting them after one another or something magical that Android does?
I think I need to override the onDraw method also for drawing the partial circle. Does that effect anything that I need to worry about?
Should I override the ScrollView and LinearLayout instead? because I think there is a lot that have been implemented in them.
Related
I'm currently trying to design a ViewGroup (specifically a subclass of FrameLayout) that can layout any number of subviews and enable them for drag/drop outside the layout group. It's almost identical to a LinearLayout:
Curently I am able to drag the views outside the ViewGroup and draw them, however after letting go of the view it can't be re-selected and further dragged around.
I'm wondering if there is a way to do this in a way that isolates the logic to the Layout subclass and doesn't involve needing to do much/any extra stuff in consuming fragments/view groups.
I've tried overriding getHitRect(Rect) in my FrameLayout subclass but it never seems to be called. dispatchTouchEvent(MotionEvent) is of course not called either, presumably because the parent ViewGroup has decided not to deliver the touch to it because it was outside the bounds. I've tried implementing a TouchDelegate as well, but I think that needs to be set on the parent view, which means needing to know about this and doing this additional step when using this Layout.
Any ideas on if/how this is possible? On iOS it can be implemented fairly easily by overriding hitTest: to take into account the frames of the child views. Is there a similar method like this on Android?
Your help is greatly appreciated!
Without any code it is very hard to see what you are doing, but best guess would be that each view should a child view of the relative layout.
I am trying to draw a View in a ViewGroup without adding it to the child list.
I am doing this because I want to display something like a ProgressBar in the exact center of layouts like a LinearLayout so I don't want the layout to handle the measuring and layouting.
I also don't want to complicate the view hierarchy by adding extra layouts just to achieve this effect so my solution was to extend the LinearLayout, create a ProgressBar and handle measuring, layouting and drawing for that view myself.
My implementation seems to work ok from what I tested but I am wondering if there is anything I am not noticing or if there are any problems that can appear in the future.
From what I understand calling addView also sets the child view's parent and calls dispatchAttachedToWindow, these methods are package-private so I can't call them myself.
Is there any side effect that can arise from calling measure, layout and draw on a view that has no parent and that was not "attached" to a window? Is there a safer way to achieve the same effect?
Thanks.
i have to draw a ball inside a view (or anything else that is good for this task).
I have followed some tutorial about the matter, but every tutorial that i have found
uses only one view (that is shown on the screen without the use of a layout).
But in my Activity i use a layout, that is composed by many views, and i want to draw
only on one of them.
here a little mockup!
Anybody knows a way to do it?
Is the view the wrong container to do it?
Thanks is advance!
Bye
...
You should extend a view that inherits from ViewGroup. this kind of view lets you handle a view that contains other views. with that, you can control the drawing, measures and layout of each of it's children separately
Your mockup can be made with a LinearLayout, which will contain some TextView's and you own View (the one which is containing the ball).
I'm surprised by this question, because there are many examples over the net explaining how to build a layout containing multiple views.
I've got the following node in my XML layout:
<com.x.y.view.TagLayout android:id="#+id/TagLayout"
android:layout_width="fill_parent" android:layout_height="170dip" />
TagLayout is a class that extends ViewGroup, and basically holds a bunch of buttons with text (a custom object that act as tags, but that's neither here or there).
The problem is that when there are too many buttons, the view no longer scrolls. I can confirm that I am adding tags but I can't see anything.
If I wrap the above within a ScrollView, the layout never renders. I tried adding the attributes android:isScrollContainer and android:scrollbars, but that doesn't change anything. Is there something I'm missing here?
I should also add that TagLayout is overriding the onMeasure() event. I guess I need to implement a scrolling mechanism there...?
Well, I mostly got it. I had to actually wrap the node above with a LinearLayout, then wrap THAT around a ScrollView. The padding is a little off (the last item is cut from the view) but it's on the right track.
You can add scrolling to your custom ViewGroup without wrapping it in multiple other layouts and views (in fact, it's better not to wrap it if you don't have to...having flat layout hierarchies is much better for performance). Just do everything described in this question, and make sure you add setWillNotDraw(false); to your custom ViewGroup constructor as described in this answer.
I've got a standard RelativeLayout laying out my Buttons and TextAreas. What I want to do now is be able to draw various sparks, flying cows etc. at arbitrary places on the screen on top of the whole thing. What's the best way to do this? Should I override onDraw() on the containing View and draw after calling super.onDraw()? Or is there some better way of drawing a layer on top?
You'll probably want to put your Relative layout and a custom view which isn't focusable and doesn't consume motionevents inside a FrameLayout, then override OnDraw in that custom view. That way you can call invalidate() on that view without making Android redraw everything else.