Note that there are some explanatory texts on larger screens.

plurals
  1. POOne view exclude another when a drop occurs
    primarykey
    data
    text
    <p>I've been struggling on the last couple of days with this program. The issue that I'm facing is about a drop event of a view which excludes another view. What the program is supposed to do is, on a click of a button, creates as many ImageButtons and they all can be dragged and dropped anywhere on the screen. However, he creates the first view and creates the second, but when I drag the second ImageButton, the first one created is deleted from the screen.</p> <p>Here is the 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" android:id="@+id/relative_main" tools:context=".WhyItActivity" &gt; &lt;Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="add button" android:onClick="addButton" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Below is the java activity responsible</p> <pre><code>public class WhyItActivity extends Activity { private RelativeLayout relative; private LayoutParams params; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_why_it); params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); relative = (RelativeLayout)findViewById(R.id.relative_main); // all the screen be a dropping area relative.setOnDragListener(new View.OnDragListener() { @Override public boolean onDrag(View view, DragEvent event) { View dragView = (View) event.getLocalState(); RelativeLayout layout = (RelativeLayout) view; switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: break; case DragEvent.ACTION_DRAG_ENTERED: break; case DragEvent.ACTION_DRAG_EXITED: break; case DragEvent.ACTION_DROP: int right = (int)event.getX(); int top = (int)event.getY(); params.setMargins(right, top, 0, 0); layout.removeView(dragView); layout.addView(dragView, params); // set back the visibility dragView.setVisibility(View.VISIBLE); break; case DragEvent.ACTION_DRAG_ENDED: break; default: break; } return true; } }); } /** OnClick */ public void addButton(View v){ // params for centralizing the image button LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.CENTER_IN_PARENT); // new object ImageButton imgBtn = new ImageButton(this); imgBtn.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); relative.addView(imgBtn, params); // letting the view be dragged around the screen imgBtn.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { ClipData data = ClipData.newPlainText("", ""); DragShadowBuilder builder = new View.DragShadowBuilder(view); view.startDrag(data, builder, view, 0); // let the view be invisible while dragging the object view.setVisibility(View.INVISIBLE); return true; } }); } </code></pre> <p>So, the question is... Where I'm screwing? I create a new object, if it's was a matter of just one view on the screen it would be working just fine.</p> <p>Thanks for reading so far, and possibly be able to help.</p>
    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.
    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