1

So my layout consists as such:

<OuterView>
    <InnerViewOne />
    <InnterViewTwo />
</OuterView>

The thing is if we were to make the two inner views as boxes with some width and height such that they overlapped then InnerViewTwo goes on the top of InnterViewOne. If there order were to be switched so that it looks like this

<OuterView>
        <InnerViewTwo />
        <InnterViewOne />
</OuterView>

then InnerViewTwo goes under InnerViewOne.

What I want visually is have InnerViewTwo under InnerViewOne but in the backend it comes after InnerViewOne. I am not worried of their place in the x and y plane. But I want to change the z.

NOTE:

1) OuterView = RelativeLayout

2) InnerViewOne = RelativeLayout

3) InnerViewTwo = LinearLayout with a vertical orientation.

someguy234
  • 559
  • 4
  • 17
  • is this ok for you? http://stackoverflow.com/questions/3740100/setting-z-order-of-view-with-bringchildtofront – lucian Nov 07 '14 at 02:55
  • doesn't that changes the index of the in the parent so it would go from the first part of my code to something like second? Which would change the whole order. I want to avoid that. – someguy234 Nov 07 '14 at 02:58
  • I think the layout will be translated to some thing like : addView, so in parent, after inflated the first layout, the children are [one, two], after bringChildToFront(View child) is called, the children in the parent is like [two, one], which is exactly the same effect after inflated the second layout I think(I didn't confirm it yet), you could read this link to check out: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3_r1/android/view/ViewGroup.java – lucian Nov 07 '14 at 03:09
  • Can you explain in more detail as an answer to this question? Thanks for your time! – someguy234 Nov 07 '14 at 04:03
  • I think I understood what you mean. But isn't that still changing the actual order of the layouts? – someguy234 Nov 07 '14 at 04:55
  • yes. it changes the order of the children in parent. please check the new answer below – lucian Nov 07 '14 at 06:21

2 Answers2

1

is this ok for you? getChildDrawingOrder

but will it confuse the users if needs some click? this method only changes the order of drawing, but not the order of event dispatching...

lucian
  • 527
  • 5
  • 15
  • Where do I call this? – someguy234 Nov 07 '14 at 07:00
  • you need extend the layout and override the method, for example: class MyRelativeLayout extends RelativeLayout {....@Override int getChildDrawingOrder... – lucian Nov 07 '14 at 07:04
  • So I need to extend `LinearLayout` and create a `new MyLinearLayout()` and in there I will override this method. Thus I will use a view inflater to add this layout? Did I understand it correctly? – someguy234 Nov 07 '14 at 07:07
  • 1
    when you want to enable this kind of z-index, please call setChildrenDrawingOrderEnabled(boolean).(or you could put this call in the constructor of the MyRelativeLayout) – lucian Nov 07 '14 at 07:07
  • 1
    Yes, your layout can be put in a layout.xml file and inflate it later. this is how we implement our widget in android. for instance, in xml , replace the with you could use this layout anywhere the LinearLayout can be used. – lucian Nov 07 '14 at 07:09
  • Any tutorial on how to control the order? I have everything working except knowing how to use the method – someguy234 Nov 07 '14 at 07:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64467/discussion-between-gurinderhans-and-lucian). – someguy234 Nov 07 '14 at 07:44
  • Thanks a lot! Glad there are nice people out there that actually take their time to help others! – someguy234 Nov 08 '14 at 05:29
0

A simpler approach is to use layers. For example move components that should be on top their own layer and simply have that layer above the original layer.

I was able to accomplish that by having a 3 RelativeLayout instances: #1 the container; #2 the bottom layer; and #3 the top layer.

<!-- Container #1 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <!-- Bottom Layer #2 -->
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    ...
  </RelativeLayout>

  <!-- Top Layer #2 -->
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    ...
  </RelativeLayout>
</RelativeLayout>
infinite-loop
  • 882
  • 11
  • 17