title: How to create a custom drag shadow?
body: <p><strong>What I want to achieve:</strong></p>
  
  <p>I want to create a drag and drop functionality in Android. I'd like to use a specific layout (different from the dragged object itself) as a drag shadow.</p>
  
  <p><strong>What result I'm getting instead:</strong></p>
  
  <p>Neither of my approaches works as expected - I end up with no visible drag shadow at all (although the target does receive the drop).</p>
  
  <p><strong>What I tried:</strong></p>
  
  <p>I tried</p>
  
  <ul>
  <li>inflating the <code>drag_item</code> layout in the activity, then passing it as an argument to the shadow builder's constructor</li>
  </ul>
  
  <p>and</p>
  
  <ul>
  <li>inflating the <code>drag_item</code> layout in the shadow builder's <code>onDrawShadow</code> method, then drawing it on the <code>canvas</code></li>
  </ul>
  
  <p><strong>Layouts:</strong></p>
  
  <p>My activity layout:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.example.app.DragDropTestActivity"
                tools:ignore="MergeRootFrame"&gt;
      &lt;TextView
          android:id="@+id/tvReceiver"
          android:text="Drop here"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/&gt;
      &lt;Button
          android:id="@+id/btnDragged"
          android:layout_height="wrap_content"
          android:text="Drag me"
          android:layout_width="match_parent"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The layout I want to use as a drag shadow:</p>
  
  <p><code>dragged_item.xml</code></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"&gt;
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Dragged Item"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Source code:</strong></p>
  
  <p>Here's the code with both approaches (represented by <code>1</code>, <code>BuilderOne</code> and <code>2</code>, <code>BuilderTwo</code>, respectively):</p>
  
  <pre><code>package com.example.app;
  
  import android.graphics.Canvas;
  import android.graphics.Point;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.view.LayoutInflater;
  import android.view.MotionEvent;
  import android.view.View;
  import android.widget.Button;
  
  public class DragDropTestActivity extends ActionBarActivity
  {
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_drag_drop_test);
          Button dragged = (Button) findViewById(R.id.btnDragged);
  
          dragged.setOnTouchListener(
              new View.OnTouchListener()
              {
                  @Override
                  public boolean onTouch(View v, MotionEvent event)
                  {
                      if (event.getAction() != MotionEvent.ACTION_DOWN) {
                          return false;
                      }
                      LayoutInflater inflater = getLayoutInflater();
  
                      int approach = 1;    
                      // both approaches fail
                      switch (approach) {
                          case 1: {
                              View draggedItem = inflater.inflate(R.layout.dragged_item, null);
                              BuilderOne builder = new BuilderOne(draggedItem);
                              v.startDrag(null, builder, null, 0);
                              break;
                          }
                          case 2: {
                              BuilderTwo builder = new BuilderTwo(inflater, v);
                              v.startDrag(null, builder, null, 0);
                              break;
                          }
                      }
  
                      return true;
                  }
              });
      }
  </code></pre>
  
  <p>My <code>BuilderOne</code> class:</p>
  
  <pre><code>    public static class BuilderOne extends View.DragShadowBuilder
      {
          public BuilderOne(View view)
          {
              super(view);
          }
  
          @Override
          public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint)
          {
              super.onProvideShadowMetrics(
                  shadowSize,
                  shadowTouchPoint);
          }
      }
  </code></pre>
  
  <p>And <code>BuilderTwo</code> class:</p>
  
  <pre><code>    public static class BuilderTwo extends View.DragShadowBuilder
      {
          final LayoutInflater inflater;
  
          public BuilderTwo(LayoutInflater inflater, View view)
          {
              super(view);
              this.inflater = inflater;
          }
  
          @Override
          public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint)
          {
              super.onProvideShadowMetrics(
                  shadowSize,
                  shadowTouchPoint);
          }
  
          @Override
          public void onDrawShadow(Canvas canvas)
          {
              final View draggedItem = inflater.inflate(R.layout.dragged_item, null);
              if (draggedItem != null) {
                  draggedItem.draw(canvas);
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>What do I do wrong?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Bounty added.</p>