I'm trying to programatically position a TextView above a custom View in a Relative Layout that has also been placed programatically (lets call this CustomView).
I've scoured these forums for similar problems, and I seem to have done all the steps, including setting ids, making sure there are no conflicting rules, etc. But yet it doesn't seem to work.
I am wondering if this has to do with the fact that my CustomView class overrides the onDraw() method (it needs to eventually be animated). In fact, testing the CustomView with it's own set of LayoutParams shows that it does not obey the rules laid out in it - in fact the way that I place this CustomView is by calling canvas.drawBitmap() with the relevant positions in the onDraw() method of CustomView.
Any idea how I can get around this problem? Is it possible to somehow place the the TextView above the CustomView?
Related
I want to set a blank relativeLayout then start adding some views from another relativeLayout.
I want to do this because at the moment I have many bitmaps in a single layout which causes memory errors. So i want to achieve the effect of adding and removing views as I see fit in the code.
At the moment I am using the setContentView() of a layout on the onCreate() which causes me to have memory erros since there are too many views to add at once.
Another way. Perhaps it is possible to setContentView() of the layout with too many views. Only making it not adding any views before I code it to add specific views.
setContentView(R.layout.start_up_screen_denmark);
// This will add all the views in the layout causing a memory error. Making everything below irrelavant.
// So perhaps there is a way to set the ContentView without adding all the views automaticly.
// Perhaps i can set a blank layout and add views to that layout from the other layout at will.
ImageView denmark = (ImageView) findViewById(R.id.GoDenmark);
ViewGroup parent = (ViewGroup) denmark.getParent();
parent.removeView(denmark);
Using a LinearLayout might be better suited for what you're trying to do since you can easily add and remove views from a LinearLayout with lin_lay.addView(View child, int index) and lin_lay.removeViewAt(int index).
However, it also sounds like you might want to look into a RecyclerView if you have a lot of bitmaps in one layout and it's causing memory issues.
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.
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 a layout which contains a ScrollView which contains various components including an ImageView. I've found that I can add a TouchListener to the ImageView and the x,y values I get for the event are offset from the corner of the ImageView (including padding). Thus I can easily handle touch events without worrying about the state of the ScrollView.
The problem is that I don't see a way to add a view on top of the ImageView so that I can make a visual change to the ImageView when it's touched in certain locations.
Specifically, there are various boxes in the image which I'd like to highlight when they're tapped. I can add a view to the activity, but I'd have to take the state of the ScrollView into account in order to display things in the right spot.
What's the best way to approach this? Can I derive a new version of ImageView, overload the draw method, and use that somehow? (not sure how I'd specify it in the xml, I'd probably need to set it in the onCreate of the activity)
You should be able to override the onDraw method of the ImageView as you suggested and draw stuff on top of the ImageView. The onDraw method gives you the Canvas that you could draw on. In the XML, you could just use the name of the new class you created instead of "ImageView" (unless you want to add parameters in addition to the ones offered by ImageView, this should be pretty straight forward).
<your.new.awesome.class.with.the.full.namespace
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/awesome_image_view"
/>
The problem is that I don't see a way to add a view on top of the ImageView so that I can make a visual change to the ImageView when it's touched in certain locations.
It seems that you are more comfortable with having a separate view that you could work on. You could do that by encapsulating the ImageView with a FrameLayout and then adding another view of any kind as a sibling of that ImageView. This view would be drawn on top of the ImageView if it was mentioned in the FrameLayout after the ImageView. This solution may not be the most efficient one, but it might get you started. Overriding the onDraw method of the ImageView would definitely be the best option.
you can make a relativelayout or something with a background as your desired image instead of having just an imageview. then you can have child views within that relativelayout.
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.