How to bring a view to front without calling bringToFront()?

20,343

Solution 1

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

Solution 2

Calling bringToFront() changes the ordering in the GridView. Try creating an ImageView with an image of the view you want to animate and animate that instead.

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
final ImageView imageView = new ImageView(getActivity());
imageView.setImageBitmap(bitmap);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(view.getWidth() , view.getHeight());
imageView.setLayoutParams(params);
rootview.addView(imageView);

Add an animation listener to your animation and remove the ImageView at the end of the animation.

Solution 3

Have you tried making the View focusable and just calling requestFocus()?

Solution 4

From API 21 you can call:

view.setZ(float)
Share:
20,343
Axarydax
Author by

Axarydax

Programming enthusiast, enjoying coding at home and at work too. I also like computer and console games. But I also have a real life too :D ! I like going to pub with friends, metal music shows, photography, driving, motorbiking and cycling. P.S. I'm keeping the April Fool's avatar!

Updated on March 07, 2020

Comments

  • Axarydax
    Axarydax about 4 years

    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.