Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BringToFront doesn't work inside a coordinator layout

Android Studio 2.0 Preview 4 

I am using to use BringToFront to get a TextView to display in front of the other controls.

The Doc's bringToFront() say you have to call requestlayout invalidate. Which I do, but doesn't work.

tvLevel.bringToFront(); tvLevel.requestLayout(); tvLevel.invalidate(); 

I am using this TextView inside a android.support.design.widget.CoordinatorLayout

However, the following code does work. But only supports API 21 and above. But I need to support API 16.

  if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {       tvLevel.setTranslationZ(4);       tvLevel.invalidate();   } 

Or by setting the xml attribute property android:translationZ("4dp") works. However, only for API 21

like image 633
ant2009 Avatar asked Dec 26 '15 08:12

ant2009


1 Answers

   /**      * Change the view's z order in the tree, so it's on top of other sibling      * views. This ordering change may affect layout, if the parent container      * uses an order-dependent layout scheme (e.g., LinearLayout). Prior      * to {@link android.os.Build.VERSION_CODES#KITKAT} this      * method should be followed by calls to {@link #requestLayout()} and      * {@link View#invalidate()} on the view's parent to force the parent to redraw      * with the new child ordering.      *      * @see ViewGroup#bringChildToFront(View)      */     public void bringToFront() {         if (mParent != null) {             mParent.bringChildToFront(this);         }     } 

according to this You may missing the line:

((View)myView.getParent()).requestLayout(); 

and it will work, Check it out.!

like image 177
Pratik Butani Avatar answered Sep 22 '22 16:09

Pratik Butani