3

I want to "Drag and Drop" items from a PopupWindow into the underlying View. The problem now is, when i dismiss the PopupWindow after a long click on an item, the underlying view does not receive any MotionEvents. This problem basically breaks down to delegating MotionEvents between views like in the drag-and-drop-between-views-issue as discussed here. A solution was proposed here but in my case this is not an option. I can't use an overlay view that lies on top of the PopupWindow, can I?

The code below is a simplified version of my problem. I basically have one button that brings up the PopupWindow. On a long click on that PopupWindow it gets dismissed. Now the MotionEvents should be send to the underlying view again, but that does not happen (as you can see in the log output).

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.PopupWindow;

public class PopupWindowDragDropActivity extends Activity implements OnClickListener, OnTouchListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.b1).setOnClickListener(this);
        findViewById(R.id.contents).setOnTouchListener(this);
    }

    @Override
    public void onClick(View v) {
        final PopupWindow p = new PopupWindow(this);
        p.setContentView(new Button(this));
        p.setHeight(100);
        p.setWidth(100);
        p.showAtLocation(findViewById(R.id.contents), Gravity.CENTER, 0, 0);
        p.getContentView().setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                v.post(new Runnable() {
                    @Override
                    public void run() {
                        p.dismiss();
                    }
                });

                return false;
            }
        });
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d("", "Action: " + event);
        return true;
    }
}

Here is the simple XML layout I use:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/contents" >

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
Community
  • 1
  • 1
Björn Hurling
  • 286
  • 1
  • 4
  • 7

0 Answers0