How to send a presentation back? How to manage z-order software?

I have a problem to send a view back. On Android, we have a method like bringToFront() to place a view on top of another view. So I want to put the view below the previous image.

Is there any method like sendToBack() or bringToBack() in Android. If so, can someone help me with this.

Note: I do not want to control the z-order in the order in which elements are placed in the layout. I want to programmatically manage a z-order.

I donโ€™t want to hide the performances at the front, I just want them to be behind a moving view.

+42
android android-layout view z-order
Jul 20 '11 at 8:26
source share
10 answers

Afaik is not for this built-in solution. I think you are trying to change the z-order in FrameLayout or something similar.

However, I think you can reorder the contained children in the layout. The RemoveChild ... () and addView () methods can take position values, so you could most likely change the children around, and that would change the z-order. However, this seems like a hacker solution.

Or consider changing the visibility property of child views, you can get similar behavior, and I think it will be much cleaner.

Edit:

With the new version of Android, L Developer Preview, it seems that, finally, we have the ability to easily change the Z-ordering of views. Properties "Elevation" and "TranslationZ" to save: https://developer.android.com/preview/material/views-shadows.html

+26
Jul 20 '11 at 9:19
source share

I understand that this was implied in the other answers, but no one sent the code. I save the following in a utility class, where I have "helper" functions for working with views:

 public static void sendViewToBack(final View child) { final ViewGroup parent = (ViewGroup)child.getParent(); if (null != parent) { parent.removeView(child); parent.addView(child, 0); } } 
+55
Nov 09 '13 at 5:43
source share

There is no sendToBack() method. But if you call bringToFront() in the lower positional view, you get almost the same effect.

+33
Feb 16 '12 at 13:37
source share

Call bringToFront() on the view you want to get in front, and then call the invalidate() method on the whole view, including the view you want at the beginning. Repeat the same for all listeners.

Therefore, when another listener is called, the previous view will be invalidated and will be in the background.

+19
Feb 21 2018-12-21T00:
source share

Here is the method I use to send the view in the opposite direction (so opposite totoTrotect, like sendToBack):

  private void moveToBack(View myCurrentView) { ViewGroup myViewGroup = ((ViewGroup) myCurrentView.getParent()); int index = myViewGroup.indexOfChild(myCurrentView); for(int i = 0; i<index; i++) { myViewGroup.bringChildToFront(myViewGroup.getChildAt(i)); } } 

Hope this helps!

+8
Jun 29 '13 at 0:45
source share

Note that you can use view.setZ(float) starting at API level 21. Here you can find additional information.

+2
Oct 23 '15 at 15:08
source share

You can also try to use the functionality of the ViewGroup.addViewInLayout() method, which takes an index, determining whether the view should be at the end or at the beginning of the list

+1
Dec 13 2018-11-11T00:
source share

it works fine like bringToBack ()

 public void moveToBack(ViewGroup viewGroup, View v) { int s = 1; for (int i = 1; i < viewGroup.getChildCount(); i++) { if (viewGroup.getChildAt(1) == v) { s = 2; } else { viewGroup.getChildAt(s).bringToFront(); } } } 
+1
Sep 21 '17 at 16:30
source share

You can try to do view.setVisibility(View.INVISIBLE) or view.setVisibility(View.GONE) .

UPDATE:

This shows how to accomplish what you want to do.

0
Jul 20 2018-11-21T00:
source share

I would either set the visibility to 0, or just let the view in front of it.

0
Jul 28 '15 at 10:25
source share



All Articles