1

I have a dialog menu setup for the user to pick a building which they wish to build. When they press the buy button, the building is automatically inserted into the activity. The issue is that when more than one is bought, then moving the most recently bought building will also move the second most recently bought building. I am not positive how to fix this issue. I would see if setting the new buildings in a frame layout rather than a linear layout would help, but my drag and drop does not work if I do it that way.

Here is the code for the imagebutton being set up after the buy button is pressed:

            layout = (LinearLayout) findViewById(R.id.newColonyHutLayout);
            buyColonyHut = (Button) buildingSelect.findViewById(R.id.buyColonyHut);
            buyColonyHut.setText("Buy");
            buyColonyHut.setBackgroundColor(-65536);
            buyColonyHut.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    //get current amount of rock and check if it is enough to buy
                    data = new Database(runGraphics.this);
                    data.open();
                    rockAmount = data.getRockAmount();
                    data.close();
                    if (rockAmount >= 100)
                    {
                        //take away spent money and update database
                        data.open();
                        rockAmount -= 100;
                        data.rockAmountEntry(rockAmount);
                        data.close();

                        //update the scores
                        updateScores();

                        //close the dialog
                        buildingSelect.dismiss();
                        buildMoreButton.setBackgroundColor(-65536);

                        //they have enough rock, build the building
                        newColonyHut = new ImageButton(runGraphics.this);
                        newColonyHut.setBackgroundResource(R.drawable.mainhut);
                        newColonyHut.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                        layout.addView(newColonyHut);
                        newColonyHut.setOnTouchListener(new OnTouchListener()
                        {   
                            @Override
                            public boolean onTouch(View v, MotionEvent event) 
                            {
                                if (event.getAction() == MotionEvent.ACTION_DOWN)
                                {
                                    newColonyHutSelected = true;
                                }//end if

                                if (event.getAction() == MotionEvent.ACTION_UP)
                                {
                                    if (newColonyHutSelected == true)
                                    {
                                        newColonyHutSelected = false;
                                    }//end if
                                }//end if
                                else if (event.getAction() == MotionEvent.ACTION_MOVE)
                                {
                                    if (newColonyHutSelected == true)
                                    {
                                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(v.getWidth(), v.getHeight());
                                        params.setMargins((int)(event.getRawX() - event.getRawY() / 5), (int) (event.getRawY() - v.getHeight()), (int) (event.getRawX() - v.getWidth() / 5), (int) (event.getRawY() - v.getHeight()));
                                        v.setLayoutParams(params);
                                    }//end if
                                }//end else

                                return false;
                            }//end onTouch function

                        });//end setOnTouchListener
                    }//end onClick function
                });//end onClickListener

here is the xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/newColonyHutLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/newColonyHut"
            android:visibility="invisible"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/mainhut" />
    </LinearLayout>
</FrameLayout>
sboehnke
  • 287
  • 1
  • 3
  • 13
  • If you want to implement drag drop, you can use drag drop provided by android. It can work on API level 11 and above. http://developer.android.com/guide/topics/ui/drag-drop.html – berserk Jun 17 '14 at 19:48
  • http://stackoverflow.com/questions/8570982/disable-or-prevent-multitouch-in-activity Check this link, maybe it will help in avoiding multi-touch. – berserk Jun 17 '14 at 19:59

1 Answers1

0

Try to return true at the end of the onTouch() Method to notify that the TouchEvent was consumed by the listener. Otherwise subsequent listeners of the View below that View that was touched will be called - e.g. the one of your second building.

Another idea would be to use the View.onDragListener, instead of implementing the drag&drop completely on your own. Here's a good example how to use it: http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html#draganddrop_overview

tknell
  • 9,007
  • 3
  • 24
  • 28