Ad

How To Get Where Is My Finger, Over Of Which Layout?

I have a block, I drag it somewhere then this cell transform to yellow then I exitted this cell, It transforms again gray but, When I drop it somewhere Everywhere is magenta, How can I paint only that block When I drop it, You can check pictures to understand my problem Best regards

public  boolean onDrag(View v,DragEvent event)
       {
           int action=event.getAction();

           switch (action)
           {
               case DragEvent.ACTION_DRAG_STARTED:
                   Log.i("Script",num+"-ACTION_DRAG_STARTED");
                   if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
                   {
                       return (true);
                   }
                   return (false);

               case DragEvent.ACTION_DRAG_ENTERED:
                   Log.i("Script",num+"-ACTION_DRAG_ENTERED");
                   v.setBackgroundColor(Color.YELLOW);
                   break;
               case DragEvent.ACTION_DRAG_LOCATION:
                    break;
               case DragEvent.ACTION_DRAG_EXITED:
                   v.setBackgroundColor(Color.GRAY);
                   break;
               case DragEvent.ACTION_DROP:
                   View view=(View) event.getLocalState();
                   ViewGroup owner=(ViewGroup) view.getParent();
                   owner.removeView(view);
                   LinearLayout container=(LinearLayout) v;
                   container.addView(view);
                   view.setVisibility(View.INVISIBLE);
                   break;

                   case DragEvent.ACTION_DRAG_ENDED:

                       v.setBackgroundColor(Color.MAGENTA);
                       break;
           }
           return true;
       }

Initial State

Drag and Exit State

Drop State

Ad

Answer

In DragEvent.ACTION_DRAG_ENDED event you should find view.getX() and view.getY(). This will return x and y co-ordinate in pixels then according to your requirement you should calculate nearest view which occupy this blocks and change its color to MAGENTA.

Ad
source: stackoverflow.com
Ad