android_logo

Android Development – bringToFront() and sendToBack() on View

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:-

View view1,view2,view3,view4; 
//code to initiate and putting the views in layout 
//Code to get view1 in foreground 
view1.bringToFront(); 
view1.invalidate(); 
view2.invalidate(); 
view3.invalidate(); 
view4.invalidate(); 
//Code to get view2 in foreground 
view2.bringToFront(); 
view1.invalidate(); 
view2.invalidate(); 
view3.invalidate(); 
view4.invalidate(); 
//Code to get view3 in foreground 
view3.bringToFront(); 
view1.invalidate(); 
view2.invalidate(); 
view3.invalidate(); 
view4.invalidate(); 
//Code to get view4 in foreground 
view4.bringToFront(); 
view1.invalidate(); 
view2.invalidate(); 
view3.invalidate(); 
view4.invalidate();

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,

Abhi,

The Creator

profile for Creator at Stack Overflow, Q&A for professional and enthusiast programmers

6 comments on “Android Development – bringToFront() and sendToBack() on View

  1. Your code didn’t work as expected, for me. Instead I did something else:
    Let’s suppose that view1, view2, view3 are direct children of parent_layout.

    view1.bringToFront();
    parent_layout.invalidate();
    ……

    This worked for me. I’m using Android 4.1.2

    • That’s also a good choice, but I’d recommend this only if invalidating separate views doesn’t work. Invalidating a complete layout might have a performance/fps issue when there’re a lot of views in the same layout.

    • Alex,
      This is also what was needed on my end in order to z-order the layouts properly. Appreciate both the initial author and your additional input greatly. Thanks guys.

  2. not working for me
    I am using surfaceview
    //for small surface
    smallSurface.bringToFront();
    smallSurface.invalidate();
    largeSurface.invalidate();

    //for large surface
    largeSurface.bringToFront();
    largeSurface.invalidate();
    smallSurface.invalidate();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s