What’s new this time?
This time there’s another most wanted answer for a most asked question. I’ve seen this question many times on programming forums and stackoverflow. The question here is to play with z ordering of the views or in laymen terms getting the bringToFront() and sendToBack() methods on a view.
CRAP!! There’s already a bringToFront() method in View class.
Yes there is. The problem is you’ll get to use it just one time on one view in a single layout. And no there’s no sendToBack() method, so you can’t send the View to back after getting it to the front.
I’ll call bringToFront() on another View and the previous one will be automatically send to background😉
*sighs* Smart,ah?? But you didn’t read it properly. You get just one bringToFront() call per layout. There’s a thing called z ordering of views and you can modify it just once. It’s not Microsoft Word where you have lot’s of methods and no layers like Photoshop :p
What’s z ordering now?
This is out of the scope of this post, however I might explain this in later posts. If you still want to know about it, just imagine it like having an order of which view comes above which other one.
Ok, I know you got the answer this time too. Just say it and let us know.
You got it this time. Well it’s not a official method, it’s a trick. Some people have given ideas like clearing the layout by calling removeAllChild() method on it and recreate the layout. However I got cleaner method than this. The trick here is to play with the z-ordering and create a new one for every view when you want to change there appearance. In the above trick they used to recreate the whole layout for this, hwoever this can be acheived by just refreshing the views.
CODE!! Where’s the code???
Yeah I know what you were waiting for. Here’s the code sample for you:
Suppose you have four views, slightly overlapping each other. The views could be any subclass of View class, such as TextView, ImageButtons, ImageView, EditText, even the layouts. Thus the code to bring each view to the front would be like this:-
//code to initiate and putting the views in layout
//Code to get view1 in foreground
//Code to get view2 in foreground
//Code to get view3 in foreground
//Code to get view4 in foreground
Oh! I almost forgot the invalidate() method. It’s a nice and simple trick, and I do not need to touch the layouts at all.
I like to do things in a simple way, and I think now you’ll get to do it the same way.
You saved my life again. Is there anything I can do for you?
Just share this post as much as you can, so that we can save more lives. You can also click on any of the share button available and share this post with your friends, collegues or anyone who can get benefit with it. You can post your questions and comments below and, can also vote up this post.
I want to hear more from you.
You can check my stackoverflow profile and look for answers I posted there (probably vote on them too😉 ). You can also ask your questions there and provide me a link to answer them. If you want a complete blog post on them, leave me a comment, and I’ll get back to you soon.
Thanks for listening,