27
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

davejal
  • 6,009
  • 10
  • 39
  • 82
ant2009
  • 27,094
  • 154
  • 411
  • 609

3 Answers3

14
   /**
     * 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.!

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
  • 1
    With me is inverse, works on android 4.4- but not work in 5.0+, just call bringToFront()+requestLayout()+invalidate()... – Gilian May 02 '16 at 00:57
9

bringToFront() work for me inside android.support.design.widget.CoordinatorLayout. My environment:

  • Android Studio 1.5.1
  • Device: Motorola with Android 4.1.2 (API 16)

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textStyle="bold"
        android:textSize="20sp"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

</android.support.design.widget.CoordinatorLayout>

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.bringToFront();
}

Here are the screenshots:

With textView.bringToFront();

enter image description here

Without textView.bringToFront();

enter image description here

BNK
  • 23,994
  • 8
  • 77
  • 87
8

Prior to KITKAT this method should be followed by calls to requestLayout() and invalidate() on the view's parent to force the parent to redraw with the new child ordering.

These methods have to be called on the view's parent. You are calling them on the view itself.

This should work.

tvLevel.bringToFront();
tvLevel.getParent().requestLayout();
tvLevel.getParent().invalidate();
Roshan
  • 3,442
  • 2
  • 14
  • 23