android how to remove specific image in grid view using drag and drop
<p>I am using drag and drop method with grid view layout.
I took 7 columns with 2 rows. Here randomly i will get 4 or 5 toys with same color. Remaining all toys are different color. I have to drop toys in bin only one color i If i drag different color toy it should not be drop in bin. It should come to its original position. If i drop same color toy it should be dropped into bin. How can i detect same color toys and how can i set same color toys should be dropped into bin? Please help...</p>

<pre><code>Here is my code...

&lt;xml code&gt;

&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_1_cr1" &gt;
//grid view to display toys 
    &lt;GridView
        android:id="@+id/grid_view"
        android:layout_width="800dp"
        android:layout_height="250dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:numColumns="7" /&gt;
//image view for bin
    &lt;ImageView
        android:id="@+id/trash_can"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_above="@+id/grid_view"
        android:layout_centerHorizontal="true"
        android:contentDescription="Delete"
        android:padding="40dip"
        android:src="@drawable/basket1"
        android:visibility="visible" &gt;
    &lt;/ImageView&gt;



&lt;/RelativeLayout&gt;

&lt;java code&gt;

package com.com.DragandDrop;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.DragEvent;

import android.view.View;
import android.view.View.OnDragListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ColorRecognition extends Activity implements OnDragListener,
        OnItemLongClickListener {
//declarations 
    ArrayList drawables;
    int[] images = new int[8];

    private BaseAdapter adapter;
    private int draggedIndex = -1;
    int Randomvalue;
//on create method
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        launchGame();

    }

    private void launchGame() {
        final Random myRandomImages = new Random();
        Randomvalue = myRandomImages.nextInt(12 - 1) + 1;

        loadImages(Randomvalue);
        GridView gridView = (GridView) findViewById(R.id.grid_view);

        gridView.setOnItemLongClickListener(ColorRecognition.this);
        gridView.setAdapter(adapter = new BaseAdapter() {

            @Override
            // Get a View that displays the data at the specified position in
            // the data set.
            public View getView(int position, View convertView,
                    ViewGroup gridView) {
                // try to reuse the views.
                ImageView view = (ImageView) convertView;
                // if convert view is null then create a new instance else reuse
                // it
                if (view == null) {
                    view = new ImageView(ColorRecognition.this);
                    view.setLayoutParams(new GridView.LayoutParams(100, 100));
                    view.setScaleType(ImageView.ScaleType.CENTER_CROP);

                }
                view.setImageResource((Integer) drawables.get(position));
                view.setTag(String.valueOf(position));
                return view;
            }

            @Override
            // Get the row id associated with the specified position in the
            // list.
            public long getItemId(int position) {
                return position;
            }

            @Override
            // Get the data item associated with the specified position in the
            // data set.
            public Object getItem(int position) {
                return drawables.get(position);
            }

            @Override
            // How many items are in the data set represented by this Adapter.
            public int getCount() {
                return drawables.size();
            }
        });
    }
//function to load images in grid view using array list
    private void loadImages(int Randomvalue) {
        switch (Randomvalue) {
        case 1:
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_1);
            drawables.add(R.drawable.boat_2);
            drawables.add(R.drawable.car_3);
            drawables.add(R.drawable.choclate_4);
            drawables.add(R.drawable.cycle_5);
            drawables.add(R.drawable.gun_6);
            drawables.add(R.drawable.hat_7);
            drawables.add(R.drawable.heli_8);
            drawables.add(R.drawable.pb_9);
            drawables.add(R.drawable.plane_10);
            drawables.add(R.drawable.scotor_1);
            drawables.add(R.drawable.bus_1);
            drawables.add(R.drawable.ted_1);
            drawables.add(R.drawable.train_1);

            Collections.shuffle(drawables);
            break;
        case 2:
            drawables = new ArrayList();
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_2);
            drawables.add(R.drawable.boat_3);
            drawables.add(R.drawable.car_4);
            drawables.add(R.drawable.choclate_5);
            drawables.add(R.drawable.cycle_6);
            drawables.add(R.drawable.gun_7);
            drawables.add(R.drawable.hat_8);
            drawables.add(R.drawable.heli_9);
            drawables.add(R.drawable.pb_10);
            drawables.add(R.drawable.plane_1);
            drawables.add(R.drawable.jeap_2);
            drawables.add(R.drawable.sumo_2);
            drawables.add(R.drawable.ted_2);
            drawables.add(R.drawable.train_2);
            Collections.shuffle(drawables);
            break;
        case 3:
            drawables = new ArrayList();
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_3);
            drawables.add(R.drawable.boat_4);
            drawables.add(R.drawable.bus_5);
            drawables.add(R.drawable.choclate_6);
            drawables.add(R.drawable.cycle_7);
            drawables.add(R.drawable.gun_8);
            drawables.add(R.drawable.hat_9);
            drawables.add(R.drawable.heli_10);
            drawables.add(R.drawable.pb_1);
            drawables.add(R.drawable.plane_2);
            drawables.add(R.drawable.scotor_3);
            drawables.add(R.drawable.sumo_3);
            drawables.add(R.drawable.ted_3);
            drawables.add(R.drawable.train_3);
            Collections.shuffle(drawables);
            break;
        case 4:
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_4);
            drawables.add(R.drawable.boat_5);
            drawables.add(R.drawable.car_6);
            drawables.add(R.drawable.choclate_7);
            drawables.add(R.drawable.cycle_8);
            drawables.add(R.drawable.gun_9);
            drawables.add(R.drawable.hat_10);
            drawables.add(R.drawable.heli_1);
            drawables.add(R.drawable.pb_2);
            drawables.add(R.drawable.jeap_3);
            drawables.add(R.drawable.scotor_4);
            drawables.add(R.drawable.sumo_4);
            drawables.add(R.drawable.ted_4);
            drawables.add(R.drawable.train_4);
            Collections.shuffle(drawables);

            break;
        case 5:
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_5);
            drawables.add(R.drawable.boat_6);
            drawables.add(R.drawable.car_7);
            drawables.add(R.drawable.choclate_8);
            drawables.add(R.drawable.cycle_9);
            drawables.add(R.drawable.gun_10);
            drawables.add(R.drawable.hat_1);
            drawables.add(R.drawable.heli_2);
            drawables.add(R.drawable.pb_3);
            drawables.add(R.drawable.plane_4);
            drawables.add(R.drawable.scotor_5);
            drawables.add(R.drawable.sumo_5);
            drawables.add(R.drawable.ted_5);
            drawables.add(R.drawable.bus_5);
            Collections.shuffle(drawables);
            break;
        case 6:
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_6);
            drawables.add(R.drawable.boat_7);
            drawables.add(R.drawable.car_8);
            drawables.add(R.drawable.choclate_9);
            drawables.add(R.drawable.jeap_10);
            drawables.add(R.drawable.gun_1);
            drawables.add(R.drawable.hat_2);
            drawables.add(R.drawable.heli_3);
            drawables.add(R.drawable.pb_4);
            drawables.add(R.drawable.plane_5);
            drawables.add(R.drawable.scotor_6);
            drawables.add(R.drawable.sumo_6);
            drawables.add(R.drawable.ted_6);
            drawables.add(R.drawable.train_6);
            Collections.shuffle(drawables);

            break;
        case 7:
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_7);
            drawables.add(R.drawable.boat_8);
            drawables.add(R.drawable.car_9);
            drawables.add(R.drawable.choclate_10);
            drawables.add(R.drawable.cycle_1);
            drawables.add(R.drawable.gun_2);
            drawables.add(R.drawable.hat_3);
            drawables.add(R.drawable.heli_4);
            drawables.add(R.drawable.bus_5);
            drawables.add(R.drawable.plane_6);
            drawables.add(R.drawable.scotor_7);
            drawables.add(R.drawable.sumo_7);
            drawables.add(R.drawable.ted_7);
            drawables.add(R.drawable.train_7);
            Collections.shuffle(drawables);
            break;
        case 8:
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_8);
            drawables.add(R.drawable.boat_9);
            drawables.add(R.drawable.car_10);
            drawables.add(R.drawable.choclate_1);
            drawables.add(R.drawable.cycle_2);
            drawables.add(R.drawable.gun_3);
            drawables.add(R.drawable.hat_4);
            drawables.add(R.drawable.jeap_5);
            drawables.add(R.drawable.pb_6);
            drawables.add(R.drawable.plane_7);
            drawables.add(R.drawable.scotor_8);
            drawables.add(R.drawable.sumo_8);
            drawables.add(R.drawable.ted_8);
            drawables.add(R.drawable.train_8);
            Collections.shuffle(drawables);
            break;
        case 9:
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_9);
            drawables.add(R.drawable.boat_10);
            drawables.add(R.drawable.car_1);
            drawables.add(R.drawable.choclate_2);
            drawables.add(R.drawable.cycle_3);
            drawables.add(R.drawable.gun_4);
            drawables.add(R.drawable.hat_5);
            drawables.add(R.drawable.heli_6);
            drawables.add(R.drawable.pb_7);
            drawables.add(R.drawable.plane_8);
            drawables.add(R.drawable.scotor_9);
            drawables.add(R.drawable.sumo_9);
            drawables.add(R.drawable.bus_9);
            drawables.add(R.drawable.train_9);
            Collections.shuffle(drawables);
            break;
        case 10:
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_10);
            drawables.add(R.drawable.boat_1);
            drawables.add(R.drawable.car_2);
            drawables.add(R.drawable.choclate_3);
            drawables.add(R.drawable.cycle_4);
            drawables.add(R.drawable.gun_5);
            drawables.add(R.drawable.jeap_6);
            drawables.add(R.drawable.heli_7);
            drawables.add(R.drawable.pb_8);
            drawables.add(R.drawable.plane_9);
            drawables.add(R.drawable.scotor_10);
            drawables.add(R.drawable.sumo_10);
            drawables.add(R.drawable.ted_10);
            drawables.add(R.drawable.train_10);
            Collections.shuffle(drawables);
            break;
        default:
            drawables = new ArrayList();
            drawables.add(R.drawable.ball_1);
            drawables.add(R.drawable.boat_2);
            drawables.add(R.drawable.car_3);
            drawables.add(R.drawable.choclate_4);
            drawables.add(R.drawable.cycle_5);
            drawables.add(R.drawable.gun_6);
            drawables.add(R.drawable.hat_7);
            drawables.add(R.drawable.heli_8);
            drawables.add(R.drawable.pb_9);
            drawables.add(R.drawable.plane_10);
            drawables.add(R.drawable.scotor_1);
            drawables.add(R.drawable.sumo_1);
            drawables.add(R.drawable.ted_1);
            drawables.add(R.drawable.train_1);
            Collections.shuffle(drawables);
            break;

        }

    }
//on drag method 
    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        switch (dragEvent.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            // Drag has started
            // If called for trash resize the view and return true
            if (view.getId() == R.id.trash_can) {
                view.animate().scaleX(1.0f);
                view.animate().scaleY(1.0f);
                return true;
            } else // else check the mime type and set the view visibility
            if (dragEvent.getClipDescription().hasMimeType(
                    ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                view.setVisibility(View.VISIBLE);
                return true;

            } else {
                return false;
            }
        case DragEvent.ACTION_DRAG_ENTERED:
            // Drag has entered view bounds
            // If called for trash can then scale it.
            if (view.getId() == R.id.trash_can) {
                view.animate().scaleX(1.5f);
                view.animate().scaleY(1.5f);
            }
            return true;
        case DragEvent.ACTION_DRAG_EXITED:
            // Drag exited view bounds
            // If called for trash can then reset it.
            if (view.getId() == R.id.trash_can) {
                view.animate().scaleX(1.0f);
                view.animate().scaleY(1.0f);
            }
            view.invalidate();
            return true;
        case DragEvent.ACTION_DRAG_LOCATION:
            // Ignore this event
            return true;
        case DragEvent.ACTION_DROP:
            // Dropped inside view bounds
            // If called for trash can then delete the item and reload the grid
            // view
            if (view.getId() == R.id.trash_can) {
                drawables.remove(draggedIndex);
                draggedIndex = -1;
            }
            adapter.notifyDataSetChanged();
        case DragEvent.ACTION_DRAG_ENDED:
            // Hide the trash can
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    findViewById(R.id.trash_can).setVisibility(View.VISIBLE);
                }
            }, 1000l);
            if (view.getId() == R.id.trash_can) {
                view.animate().scaleX(1.0f);
                view.animate().scaleY(1.0f);
            } else {
                view.setVisibility(View.VISIBLE);
            }
            // remove drag listeners
            view.setOnDragListener(null);
            return true;

        }
        return false;
    }
//long click method
    @Override
    public boolean onItemLongClick(AdapterView gridView, View view,
            int position, long row) {
        ClipData.Item item = new ClipData.Item((String) view.getTag());
        ClipData clipData = new ClipData((CharSequence) view.getTag(),
                new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN }, item);
        view.startDrag(clipData, new View.DragShadowBuilder(view), null, 0);
        View trashCan = findViewById(R.id.trash_can);
        trashCan.setVisibility(View.VISIBLE);
        trashCan.setOnDragListener(ColorRecognition.this);

        trashCan.setOnDragListener(ColorRecognition.this);
        draggedIndex = position;
        return true;
    }

}
</code></pre>
