Android – How to bring a view to front without calling bringToFront()

androidlayoutviewviewgroupz-index

There is apparently a bug in Android which breaks View.bringToFront.

If I have Views arranged in a GridView and want to bring a View to front by calling bringToFront(), it may get moved to the bottom right position in the GridView.

Something shady is going on there, so I can't use bringToFront(). When I don't call it, everything works fine. But the Views overlap – if I use scale animation to scale up a View, it's partially obscured by the Views to the bottom and to the right.

I've checked out bringToFront's source and it calls parent.bringChildToFront(…)

it's this method

public void bringChildToFront(View child) {
     int index = indexOfChild(child);
      if (index >= 0) {
          removeFromArray(index);
          addInArray(child, mChildrenCount);
          child.mParent = this;
      }
  }

it apparently removes the View from itself! Without mercy! Is a ViewGroup so dumb that it can't manage Z-indexes in any other way that shuffling controls around? Obviously when GridView removes its child and then adds it back again, it gets placed at the end of the Grid!

Is there anything I can do to show a View on top of others without resorting to some hacking? One thing that comes to my mind is to create another View above the GridView, which will appear to be above the one I'm trying to bringToFront(), but I don't like this solution.

Best Answer

apparently I missed the android:clipChildren="false" property in GridView. It solves my problem.

Related Topic