Forum Xamarin.Android

Drag and Drop in Android C#

I am so new on C# and I just want to make a simple app with only using Xamarin Android.
I have created my Native Android code. I just want you to help to translate the same idea or code to C# using Xamarin Android in VS.

Native android code so far:

MainActivity.java
`
public class MainActivity extends AppCompatActivity
{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    findViewById(R.id.imageView).setOnTouchListener(new MyTouchListener());
}

private final class MyTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }
}

class MyDragListener implements View.OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                // do nothing
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                break;
            case DragEvent.ACTION_DROP:
                View view = (View) event.getLocalState();
               // ViewGroup owner = (ViewGroup) view.getParent();
               // owner.removeView(view);
                RelativeLayout container = (RelativeLayout) v;
                container.addView(view);
                view.setVisibility(View.VISIBLE);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
            default:
                break;
        }
        return true;
    }
}

`

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.travana.burakmalkoc.moveup.MainActivity" tools:showIn="@layout/activity_main" android:id="@+id/LLayout" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> </RelativeLayout>

Answers

  • JimBennettJimBennett GBXamarin Team, Insider, University, Developer Group Leader ✭✭✭✭

    Create an android app, and code it up using the same API's just in C#. You might first want to learn a bit of C# first and read some of the Xamarin developer docs.

  • Step 1. Implement View.IOnTouchListener in your activity
    Step 2. Copy and Paste this code
    public bool OnTouch(View view, MotionEvent e)
    {
    /*
    * Sequence if button was click is MotionEventActions.Down followed by MotionEventActions.Up
    * Sequence if button was moved is MotionEventActions.Down followed by MotionEventActions.Move
    */
    switch (e.Action)
    {
    case MotionEventActions.Down:
    ftX = e.GetX();
    ftY = e.GetY();
    btnTrackedIsClicked = true;
    break;
    case MotionEventActions.Up:
    if (btnTrackedIsClicked)
    {
    showMessage("Clicked");
    btnTrackedIsClicked = false;
    }
    break;
    case MotionEventActions.Move:
    btnTrackedIsClicked = false;
    // RawX = (xAxis of of the view starting from the left of main view - view width) - xAxis where the first touch started(starting from left to right of the view)
    // RawY = view height - yAxis where the first touch started(starting from top to bottom of the view)
    float halfViewWidth = view.Width / 2;
    float left = (e.RawX - halfViewWidth);
    float top = ((e.RawY - view.Height));
    if ((e.RawX + view.Width) - halfViewWidth <= parentLayout.Width && (e.RawX + view.Width) - halfViewWidth >= view.Width)
    {
    view.SetX(left);
    }
    if (top >= 0 && top <= parentLayout.Height - view.Height)
    {
    view.SetY(top);
    }
    break;
    }

            return true;
        }
    
Sign In or Register to comment.