Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid beginner - Place Image at DragEvent
    primarykey
    data
    text
    <p>I realize that similar questions have been posted, and I've viewed them and lots of other topics etc to find a solution - I'm clearly missing the obvious - as I am still learning the basics!</p> <p>Goal: Simple drag and drop. User moves image across screen and either drops on top of another image or anywhere on the screen.</p> <p>API: >11</p> <p>Completed:</p> <ul> <li>Can drag the image and place on top of another image and get response (using Toast to confirm).</li> <li>Can drag the image anywhere on screen and get response (using Toast to confirm).</li> </ul> <p>NOT working:</p> <ul> <li>Cannot drag image anywhere on screen and deposit image where finger lifted</li> </ul> <p>I have tried lots of different methods but always compiling errors. Looking at my code, could anyone recommend a clean and simple method to place a image at ACTION_DRAG_ENDED (keep in mind I am a beginner) </p> <p>Here is my java code:</p> <pre><code>public class MainActivity extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } boolean okToDrop; /** Called when the activity is first created. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.oval).setOnTouchListener(new theTouchListener()); findViewById(R.id.square).setOnTouchListener(new theTouchListener()); findViewById(R.id.robot).setOnTouchListener(new theTouchListener()); findViewById(R.id.oval).setOnDragListener(new theDragListener()); findViewById(R.id.square).setOnDragListener(new theDragListener()); findViewById(R.id.robot).setOnDragListener(new theDragListener()); } private final class theTouchListener implements OnTouchListener { public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { ClipData data = ClipData.newPlainText("", ""); DragShadowBuilder shadowBuilder = new View.DragShadowBuilder( view); view.startDrag(data, shadowBuilder, view, 0); view.setVisibility(View.VISIBLE); return true; } else { return false; } } } class theDragListener implements OnDragListener { @Override public boolean onDrag(View view, DragEvent dragEvent) { int dragAction = dragEvent.getAction(); View dragView = (View) dragEvent.getLocalState(); if (dragAction == DragEvent.ACTION_DRAG_EXITED) { okToDrop = false; } else if (dragAction == DragEvent.ACTION_DRAG_ENTERED) { okToDrop = true; } else if (dragAction == DragEvent.ACTION_DRAG_ENDED) { if (dropEventNotHandled(dragEvent) == true) { // Code to generate image goes here *********************** Context context = getApplicationContext(); CharSequence text = "Action Dropped Not In Box!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); dragView.setVisibility(View.VISIBLE); } } else if (dragAction == DragEvent.ACTION_DROP &amp;&amp; okToDrop) { ImageView i = (ImageView) findViewById(R.id.square); i.setImageResource(R.drawable.oval); dragView.setVisibility(View.INVISIBLE); Context context = getApplicationContext(); CharSequence text = "Action Resulted In It Being Dropped In The Box"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); } return true; } private boolean dropEventNotHandled(DragEvent dragEvent) { return !dragEvent.getResult(); } } </code></pre> <p>And my xml:</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" &gt; &lt;ImageView android:id="@+id/square" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/square_box" /&gt; &lt;ImageView android:id="@+id/oval" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="58dp" android:layout_marginTop="58dp" android:src="@drawable/oval" /&gt; &lt;ImageView android:id="@+id/robot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/oval" android:layout_below="@+id/square" android:layout_marginTop="70dp" android:src="@drawable/ic_launcher" /&gt; &lt;/RelativeLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload