title: Android, view.bringToFront() not working in ViewPager
body: <p>I have a ViewPager modelled after <a href="http://developer.android.com/training/animation/screen-slide.html" rel="nofollow noreferrer">this</a> example, where I am implementing <code>ViewPager.PageTransformer</code>:</p>
  
  <pre><code>public class DepthPageTransformer implements ViewPager.PageTransformer {
      private static final float MIN_SCALE = 0.75f;
  
      public void transformPage(View view, float position) {    
          if (position &lt; -1) { // [-Infinity,-1)
              view.setAlpha(0);
          } else if (position &lt;= 0) { // [-1,0]
  
              view.setAlpha(1);
              view.setTranslationX(0);
              view.setScaleX(1);
              view.setScaleY(1);
  
          } else if (position &lt;= 1) { // (0,1]
  
              view.setAlpha(1 - position);
              view.setTranslationX(view.getWidth() * -position);
              float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
              view.setScaleX(scaleFactor);
              view.setScaleY(scaleFactor);
  
          } else { // (1,+Infinity]
              view.setAlpha(0);
          }
      }
  }
  </code></pre>
  
  <p>and it works great, just as shown in the <strong>Depth Page Transformer</strong> animation shown in the link above.</p>
  
  <p>But, the way it currently works is that views to the <em>right</em> are always below the views on the <em>left</em>. As you scroll right, the views come from below. Scrolling left has the current view going below the new view, which slides in on top.</p>
  
  <p>What I'd like to do is have the current view ALWAYS be on top, and for the new view (left or right) to come in from behind/below.</p>
  
  <p>I've tried adding</p>
  
  <pre><code>view.bringToFront();
  </code></pre>
  
  <p>to the <code>if(position &lt;= 0)</code> block, but nothing changes. I've also tried adding</p>
  
  <pre><code>view.bringToFront();
  view.invalidate();
  </code></pre>
  
  <p>and</p>
  
  <pre><code>view.bringToFront();
  ((View)view.getParent()).invalidate();
  </code></pre>
  
  <p>all to no avail. These are the same approaches taken by <a href="https://stackoverflow.com/questions/28672992/android-viewpager-with-depthpagetransformer-z-index-issue">similar SO posts</a> that I can find, but all of them have accepted answers and comments saying they work. But mine doesn't!</p>
  
  <p>How can I have the current view ALWAYS be the top-most view, regardless of swipe direction?</p>
  
  <p><strong>EDIT 1 - ADDITIONAL DETAILS</strong></p>
  
  <p>As was mentioned in the comments, I'd need to know if swiped right or left to get the same animation going both ways.</p>
  
  <p>The ViewPager only has <em>three</em> views, and they are presented as a <code>VerticalViewPager</code>, based off <a href="https://stackoverflow.com/questions/13477820/android-vertical-viewpager">this</a> post.</p>
  
  <p>Within the PageTransformer, I have the following:</p>
  
  <pre><code>    @Override
      public void transformPage(View view, float position) {
  
          if (position &lt; -1) { // [-Infinity,-1)
              view.setAlpha(0);
          }
          else if(position &lt;= 0){
              if(view == first){
                  view.setAlpha(1 - Math.abs(position));
                  view.setTranslationX(view.getWidth() * -position);
                  view.setTranslationY(0);
                  float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
                  view.setScaleX(scaleFactor);
                  view.setScaleY(scaleFactor);
              }else if(view == second){
                  view.setAlpha(1);
                  view.setTranslationX(view.getWidth() * -position);
                  float yPosition = position * view.getHeight();
                  view.setTranslationY(yPosition);
              }else if(view == third){
                  // Never happen
              }
          }
          else if (position &lt;= 1) { // [0,1]
              if(view == first){
                  // Never happen
              }else if(view == second){
                  view.setAlpha(1);
                  view.setTranslationX(view.getWidth() * -position);
                  float yPosition = position * view.getHeight();
                  view.setTranslationY(yPosition);
              }else if(view == third){
                  view.setAlpha(1 - position);
                  view.setTranslationX(view.getWidth() * -position);
                  view.setTranslationY(0);
                  float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
                  view.setScaleX(scaleFactor);
                  view.setScaleY(scaleFactor);
              }
          } else { // (1,+Infinity]
              view.setAlpha(0);
          }
  
      }
  </code></pre>
  
  <p>Where <code>first</code>, <code>second</code> and <code>third</code> are type <code>View</code> and set inside of the PagerAdapter:</p>
  
  <pre><code>@Override
  public View instantiateItem(ViewGroup container, int position) {
      if(position == 0){
          ... stuff ...
          vvp.first = img;
          ... stuff ...
      }else if(position == 1){
          ... stuff ...
          vvp.second = img;
          ... stuff ...
      }else if(position == 2){
          ... stuff ...
          vvp.third = img;
          ... stuff ...
      }
  }
  </code></pre>
  
  <p>It's a little hacked together, but works great and gets exactly the functionality I'm looking for. EXCEPT for the <code>bringToFront</code> thing that isn't working. But it doesn't work whether I use the <code>transformPage</code> code in the original post, or the more comprehensive version in this edit.</p>
  
  <p>Still unsure how to make <code>second</code> (i.e. the middle view of the three) always on top. I even tried a generic</p>
  
  <pre><code>second.bringToFront();
  </code></pre>
  
  <p>at the top of the method, but still nothing.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Just tried <code>view.setZ()</code> but met the following error:</p>
  
  <blockquote>
    <p>java.lang.NoSuchMethodError: android.view.View.setZ</p>
  </blockquote>
  
  <p>So that option's out as well.</p>
  
  <p><strong>EDIT 3 - SOLUTION</strong></p>
  
  <p>For my solution to this problem, please see answer below (using <code>getChildDrawingOrder</code>).</p>
  
  <p>That being said, I still cannot get <code>bringToFront</code> to work, and will happily select your answer as the chosen one if you can figure it out.</p>
  
  <p>Thanks again in advance.</p>