Svarog
Svarog

Reputation: 2208

Android on DragEvent fires when dragging anywhere in the screen

I have a simple layout that becomes visible once a drag event starts:

<RelativeLayout
    android:id="@+id/onDragMenu"
    android:visibility="gone"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="blablabla"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_marginTop="75dp"
        android:textAlignment="center"
        />
    <ImageView
        android:id="@+id/smsDragButton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="25dp"
        android:layout_marginBottom="25dp"
        android:src="@mipmap/sms_icon"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:background="@drawable/rounded_button"
        android:padding="5dp"
        />
    <ImageView
        android:id="@+id/callDragButton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="25dp"
        android:layout_marginBottom="25dp"
        android:src="@mipmap/phone_icon"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:background="@drawable/rounded_button"
        android:padding="5dp"
        />
</RelativeLayout>

And some code to handle what happens when something is dragged onto one of the image views:

View callButton = onDragMenu.findViewById(R.id.callDragButton);
callButton.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
    switch (event.getAction()){
        case DragEvent.ACTION_DRAG_ENTERED:
            // blablabla
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            // blablabla
            break;
    }
    return true;
}

});

However, onDrag is triggered the second I start the drag(outside the buttons), with ACTION_DRAG_STARTED, and ACTION_DRAG_ENTERED is never hit. Finally, when I let go, ACTION_DRAG_ENDED is hit, with coordinates (0,0).

I'm quite new to Android, and probably missing something basic. Please help. Thanks

Upvotes: 1

Views: 88

Answers (2)

Afshin
Afshin

Reputation: 977

Set the visibility of Relative layout to INVISIBLE instead of gone.

Upvotes: 2

Sai Aditya
Sai Aditya

Reputation: 2393

Instead of Using Drag event,Use onTouch drag event.

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements View.OnTouchListener {

    private ImageView mImageView;
    private ViewGroup mRrootLayout;
    private int _xDelta;
    private int _yDelta;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRrootLayout = (ViewGroup) findViewById(R.id.root);
        mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150);
        mImageView.setLayoutParams(layoutParams);
        mImageView.setOnTouchListener(this);
    }

    public boolean onTouch(View view, MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
            _xDelta = X - lParams.leftMargin;
            _yDelta = Y - lParams.topMargin;
            break;
        case MotionEvent.ACTION_UP:
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            break;
        case MotionEvent.ACTION_POINTER_UP:
            break;
        case MotionEvent.ACTION_MOVE:
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                    .getLayoutParams();
            layoutParams.leftMargin = X - _xDelta;
            layoutParams.topMargin = Y - _yDelta;
            layoutParams.rightMargin = -250;
            layoutParams.bottomMargin = -250;
            view.setLayoutParams(layoutParams);
            break;
        }
        mRrootLayout.invalidate();
        return true;
    }
}

Creating View layout activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/activity_vertical_margin" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="124dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

Upvotes: 0

Related Questions