title: How to show a Popup Window above specified Linear Layout
body: <p>I'm trying to show a popup window above my linear layout but it is not showing at expected position. </p>
  
  <p>It is taking my main linear layout and calculating the gravity.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      Button button;
      PopupWindow popup;
      LinearLayout middle;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          middle = (LinearLayout) findViewById(R.id.middle);
          button = (Button) findViewById(R.id.button);
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  final View layout = getLayoutInflater().inflate(R.layout.mypopup, null);
  
                  popup = new PopupWindow(layout);
                  popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
                  popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
                  popup.setOutsideTouchable(true);
                  popup.setFocusable(true);
                  popup.setBackgroundDrawable(new BitmapDrawable());
  
                  popup.showAtLocation(middle, Gravity.TOP, 0, middle.getHeight());
  
                  popup.showAsDropDown(middle);
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>Layout</strong></p>
  
  <pre><code>&lt;LinearLayout android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
              &lt;LinearLayout
                  android:id="@+id/viewA"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="0.5"
                  android:background="#0094d6"
                  android:orientation="horizontal"
                  android:gravity="center_vertical|center_horizontal"&gt;
  
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="#000000"
                  android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingStart="@dimen/activity_horizontal_margin"
                  android:id="@+id/middle"
                  android:orientation="horizontal"&gt;
  
                  &lt;Button
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:id="@+id/button"
                      android:text="show popup"/&gt;
  
              &lt;/LinearLayout&gt;
  
              &lt;LinearLayout
                  android:id="@+id/viewB"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="0.5"
                  android:orientation="horizontal"
                  android:background="#987f5a"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Popup Layout:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#ffffff"&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="I'm a Popup"
          android:textSize="20dp"
          android:textStyle="bold"
          android:background="#000000"
          android:textColor="#ffffff"
          android:padding="20dp"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Problem is, popup is showing at the top of Activity. I want to show it at the top of middle layout. </p>
  
  <p>Where am I doing it wrong?
  Thank You</p>