Opposite of View.bringToFront() in Android - android

I was wondering if there is some opposite method of View.bringToFront()?
When I call bringToFront() my whole screen get locked cause i have some problem with overriding the onMeasure() method(when I implement onMeasure, my custom View doesn't draw itself even it enters onDraw method) and my custom View takes up the whole screen ...
I badly need bringToFront() but I need to start some animation that lasts for three seconds and by calling bringToFront() user input doesn't work anymore.
Can I reset that bringToFront()?

I was also looking for a sendToBack() method. However, this does not exist in the current API.

call bringToFront() on the view you want to get in the front, and then call the invalidate() method on all the view including the view which you want in the front. Repeat same for all the listeners. So when another view's listener will get called, the previous view will get invalidated and will be in background.

Related

Android View bizarre behaviour

I have this bizarre behaviour when calling invalidate.
I have made a custom viewgroup containing a bunch of imageviews.
I set up a onTouch listener with the on touch method to scroll the view.
I know all the code is correct in terms of distances to scroll etc. by using logcat.
Now, for some reason the onLayout method is not being called after I call this.invalidate(). When I try to scroll, nothing changes on the screen. However, after I pause the activity and resume it, the screen will have shifted by the amount I scrolled.
I am not blocking the UI thread because there is nothing to block it with. In any case, I have tried postInvalidate() and nothing has worked.
Anybody have any bloody idea what's going on?
invalidate() does not relayout the view, it only causes the view to be redrawn. If you want to relayout it (which will invoke onLayout()) you also have to make a call to requestLayout().

View.onDraw() --- when does it get called?

I put a Log.d() call into the onDraw() of my extended View, so I could see how often and when it's getting called. It gets called upon instantiation of the view, which is not surprising. But then I notice, it gets called on every tap that is handled by onTouchEvent(), even though my code there isn't doing anything remotely related to graphics. However, in the documentation for Views, I can't seem to find anything about when onDraw() is actually called. I'm not really concerned about my particular project here (this doesn't cause a problem for me), I would just like to know if there is a list somewhere or something that shows the order of operations for a View, particularly what causes onDraw() to get called.
AFAIK, a View's onDraw() is called when:
The view is initially drawn
Whenever invalidate() is called on the view
Invalidate can be called by you or the system whenever needed. For example, a lot of Views change how they look onTouch, like an EditText getting an outline and cursor, or a button being in the pressed state. Due to this, Views are redrawn on touch.
I agree that it would be nice to have a document that detailed the working of Views, and if one exists and somebody knows where to find it, please let us know.
onDraw() is called when invalidate() is called.
But you should know for ViewGroups: onDraw() will not be called like you expect. Rather, onDispatchDraw().
However, in a ViewGroup you can call setWillNotDraw(false) in the constructor to make onDraw() to be called on invalidate().
Take a look at this answer
If you set a background drawable for a View, then the View will draw
it for you before calling back to its onDraw() method.
onAttachedToWindow () is called when the view is attached to a window.
At this point it has a Surface and will start drawing. Note that this
function is guaranteed to be called before
onDraw(android.graphics.Canvas), however it may be called any time
before the first onDraw -- including before or after onMeasure(int,
int).
invalidate() mark the area defined by dirty as needing to be drawn. If
the view is visible, onDraw(android.graphics.Canvas) will be called at
some point in the future.
One important thing to keep in mind is that try to minimize calling of invalidate() function with no arguments.
Instead try to maximize the invalidate() function with four arguments.As drawing whole view is very expensive.The second variant refreshes only the part of view.
Additional to the above: The soft keyboard causes a View.invalidate()-->View.onDraw() sequence after resizing the Window to sensibly accommodate the 'keyboard'. A custom View.onDraw() must leave itself in a state that anticipates this possibility.
Such phenomenum explains why the app you developed and tested on a tablet with a bluetooth keyboard went to the dogs once it reached the real world (-:
When is onDraw called (check this for more details)
The onDraw method is called whenever android thinks that your view
should be redrawn. This can be tha case when your view is animated, in
which case onDraw is called for every frame in the animation. It is
also called when the layout changes and your view is re-positioned on
the screen.
But what if some data inside your view has changed and you want to
make sure that the view is redrawn. You can’t call onDraw directly.
Instead you should call invalidate to tell the view that it needs to
redraw itself.

Confused with removeAllViewsInLayout(), postInvalidate() and refreshDrawableState()

I am really confused with this three functions: removeAllViewsInLayout(), postInvalidate() and refreshDrawableState().
removeAllViewsInLayout()
When I use removeAllViewsInLayout() in my program, all views were gone. But when trigger postInvalidate() to refresh, nothing. I think removeAllViewsInLayout() deletes all of my views. Is there a way to clear all things in my view but not delete it?
postInvalidate()
I want to refresh my view. But with refreshDrawableState(), I can do only once, why?
If you call postInvalidate() (or just call invalidate() if on the UI thread), the system will schedule a redraw of your view. Your onDraw() method can then do whatever it wants, including just drawing a blank canvas.
If you call removeAllViewsInLayout() it will do just that -- remove every view, including your custom view. You probably don't want to do that. (EDIT: removeAllViewsInLayout() is intended to be called only as part of layout calculations for a ViewGroup. A typical use would be by a ViewGroup that "owns" a large number of views but during onLayout() processing decides to display only those children that actually fits on the screen. If you are not in the process of computing the view layout, you should call removeAllViews() instead.)
Calling refreshDrawableState() is only useful if your view is using a Drawable object that is sensitive to the state of the view. For example, a Button will use this so that the background drawable changes color when the button is pressed. For what you are doing, you don't need to bother with this method, either.

Android custom view is not drawn properly

I have a custom widget based on the Android Gallery view. It is slightly customized to show the elements from the left of the view without a gap and pass required events to my listener.
On the very first view layout everything works perfect. However when I pause and resume the activity the view freezes. It is visible, I can see OnTouch() event handlers working, view positions adjusted, but the picture is not changing. The OnDraw() method for the Gallery widget is not called at all!
I have another widget in this layout (a sliding drawer) and as soon as I barely move the drawer everything starts working back again as it should.
I am completely out of ideas how to fix this. Tried different ways to force the OnDraw() method but it is just not working.
Any ideas?
Thanks
Make sure you override onDraw(android.graphics.Canvas) and not OnDraw() (both the Canvas argument and the lower case initial 'o').
If it's not that, then please post some code to look at it.

Android - onDraw not being called in dynamically inserted custom view

I have a custom view being dynamically inserted and the onDraw is not being called. I initially made the custom view without the dynamic insertion and it worked fine, however when moving it to my working code base, I cannot get onDraw to get called ever, even though the view constructor gets called and the onMeasure also gets called. I've tried putting invalidate() and postInvalidate() calls around the place, but they don't seem to force the view to draw. The view hierarchy looks the same for both version and I'm lost.
Can anyone help?
EDIT: A bit more testing has shown that onDraw is getting called on the emulator, but not the handset. Also onSizeChanged on the handset has the correct values coming through, but on the emulator the values are all 0.
EDIT: I've been playing about with it and still can't get anywhere, so have put a bounty on it. If you need more code / xml / info then please let me know.
This sounds a lot like a problem i had recently, with my first custom view. Did you notice if, when loading view hierarchy, your custom views show up? and anyway do they appear laid out in the wireframe preview or not?
If the latter is the case, it's requestLayout() that doesn't get called (loading view hierarchy calls that on every view - that's why it makes views show up). i've also found that a similar bug was reported already here.
I changed the View to a TextView and it seems to work now, although I'm a little confused as to why the View didn't work.

Categories

Resources