Drag and Drop in Android

+2 Wilford Ice Brimley · November 17, 2014
Hello everyone,

So I am creating an app where I have three pictures and a framelayout below those pictures. My goal is to drag the correct picture(one is right the other two are wrong) into the framelayout. This would then make a button appear so I could move on to the next screen. I have the drag and drop feature working and I even have to framelayout able to detect that something was dropped into it. My problem is that I can't figure out how to make the framelayout detect that I dropped the correct image into it (it thinks all of the images are correct). I tried giving them all the image tags but that didn't seem to help. Any ideas?

Thank you


public class PageOne extends Activity {
    ImageButton pageArrow;
    ImageView leftImage;
    ImageView centerImage;
    ImageView rightImage;
    FrameLayout answerBox;
    TextView turnPageText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_one);

        pageArrow =(ImageButton)findViewById(R.id.pg1Arrow);
        leftImage =(ImageView)findViewById(R.id.pg1Pic1);
        centerImage =(ImageView)findViewById(R.id.pg1Pic2);
        rightImage =(ImageView)findViewById(R.id.pg1Pic3);
        answerBox = (FrameLayout)findViewById(R.id.pg1FrameLayout);
        turnPageText = (TextView)findViewById(R.id.pg1TurnPage);
        leftImage.setTag("correct");
        centerImage.setTag("wrong");
        rightImage.setTag("wrong");
        turnPageText.setVisibility(View.INVISIBLE);
        pageArrow.setAlpha((float) 0.4);


        pageArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PageOne.this, PageTwo.class));
            }
        });
        pageArrow.setClickable(false);

        answerBox.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                final int action = event.getAction();
                switch (action){
                    case DragEvent.ACTION_DRAG_STARTED:
                        return true;
                    case DragEvent.ACTION_DRAG_EXITED:
                        answerBox.setBackground(getResources().getDrawable(R.drawable.custom_border));
                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        answerBox.setBackgroundColor(Color.parseColor("#CCCCCC"));

                        break;
                    case DragEvent.ACTION_DROP:
                        if(v.getTag() == "correct") {
                            pageArrow.setClickable(true);
                            pageArrow.setAlpha((float) 1.0);
                            turnPageText.setVisibility(View.VISIBLE);
                        }
                        return true;
                    case DragEvent.ACTION_DRAG_ENDED:


                        answerBox.setBackground(getResources().getDrawable(R.drawable.custom_border));
                        break;
                    default:
                        return false;
                }
                return true;
            }
        });

        leftImage.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ClipData data = ClipData.newPlainText("correct", "correct");
                View.DragShadowBuilder shadow = new View.DragShadowBuilder(leftImage);
                v.startDrag(data,shadow,null, 0);
                return false;
            }
        });

        centerImage.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ClipData data = ClipData.newPlainText("","");
                View.DragShadowBuilder shadow = new View.DragShadowBuilder(centerImage);
                v.startDrag(data,shadow,null, 0);
                return false;
            }
        });

        rightImage.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadow = new View.DragShadowBuilder(rightImage);
                v.startDrag(data, shadow, null, 0);
                return false;
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Wilford Ice Brimley · November 18, 2014
Never mind, I figured it out! All you have to do is create a flag field and have the item you want to be correct have a value of 1 and the rest have a value of 2. 
  • 1

Java / Android Development

106,943 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator