rocket_appliance
rocket_appliance

Reputation: 13

Android Drag and Drop Implementation

I am a beginner with android and I am trying to make a simple word scramble game. For now, I'm just trying to figure out how to use the drag and drop functionality. I went through some tutorials and tried to apply what I learned, but my program keeps crashing when I try to drop one of the letter tiles into it's target.

The runtime error I keep getting is

java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.LinearLayout

It's saying that the error is occurring on this line of the java code

LinearLayout newParent = (LinearLayout)v;

But that line in the tutorials I've done works just fine. Obviously I'm doing something wrong. I've just been stuck on this since yesterday and would really appreciate some help.

Here is my xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="41dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1"
android:paddingLeft="0dp"
android:paddingRight="10dp">


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="0:59"
    android:id="@+id/textViewTimer"
    android:editable="false"
    android:gravity="center_horizontal"
    android:textSize="50dp"
    android:textStyle="bold" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:paddingTop="60dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_target1"
            android:src="@drawable/blank_space"
            android:cropToPadding="false"
            android:baselineAlignBottom="false"
            android:layout_weight="1"
            android:padding="3dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_target2"
            android:src="@drawable/blank_space"
            android:cropToPadding="false"
            android:baselineAlignBottom="false"
            android:layout_weight="1"
            android:padding="3dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_target3"
            android:src="@drawable/blank_space"
            android:cropToPadding="false"
            android:baselineAlignBottom="false"
            android:layout_weight="1"
            android:padding="3dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_target4"
            android:src="@drawable/blank_space"
            android:cropToPadding="false"
            android:baselineAlignBottom="false"
            android:layout_weight="1"
            android:padding="3dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_target5"
            android:src="@drawable/blank_space"
            android:cropToPadding="false"
            android:baselineAlignBottom="false"
            android:layout_weight="1"
            android:padding="3dp" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="156dp"
    android:paddingBottom="0dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_letter1"
        android:src="@drawable/letter_a"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_letter2"
        android:src="@drawable/letter_g"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_letter3"
        android:src="@drawable/letter_r"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_letter4"
        android:src="@drawable/letter_e"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView_letter5"
        android:src="@drawable/letter_v"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />
</LinearLayout>
</LinearLayout>

And here is my java code

public class MainActivity extends Activity {

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

    //set OnTouchListeners for the letters
    findViewById(R.id.imageView_letter1).setOnTouchListener(new     MyTouchListener());
    findViewById(R.id.imageView_letter2).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.imageView_letter3).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.imageView_letter4).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.imageView_letter5).setOnTouchListener(new MyTouchListener());

    //set OnDragListeners for the drop targets
    findViewById(R.id.imageView_target1).setOnDragListener(new MyDragListener());
    findViewById(R.id.imageView_target2).setOnDragListener(new MyDragListener());
    findViewById(R.id.imageView_target3).setOnDragListener(new MyDragListener());
    findViewById(R.id.imageView_target4).setOnDragListener(new MyDragListener());
    findViewById(R.id.imageView_target5).setOnDragListener(new MyDragListener());
}

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 {
    Drawable emptySpace = getResources().getDrawable(R.drawable.blank_space);

    @Override
    public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        switch (action) {
            case DragEvent.ACTION_DRAG_STARTED:
                //do nothing
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
             //   //could highlight drop target box
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                //un-highlight
                break;
            case DragEvent.ACTION_DROP:
                //dropped
                View view = (View)event.getLocalState();
                LinearLayout oldParent = (LinearLayout)view.getParent();
                oldParent.removeView(view);
                LinearLayout newParent = (LinearLayout)v;
                newParent.addView(view);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                v.setBackgroundDrawable(emptySpace);
                break;
            default:
                break;
        }
        return true;
    }
}

}

Upvotes: 1

Views: 93

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007228

You are calling setOnDragListener() on ImageView widgets. Hence, the View passed into onDrag() will be one of those ImageView widgets, not a LinearLayout.

Upvotes: 1

Related Questions