1

I am working on a photo editor application, in which i am trying to drag and drop an edittext, in which user can enter any string.I have applied touch listener to drag the view, it runs perfectly for the first time but when i drag and drop the edittext second time it gets invisible when the user drops the view.

Here is my code snippet :

This is my editText

        <RelativeLayout 
            android:layout_below="@+id/top"
            android:id="@+id/middle1"
            android:layout_width="wrap_content"
            android:layout_height="420dp"
            >
             <EditText
                android:id="@+id/screen"
                android:layout_width="200dp"
                android:layout_height="100dp"
                android:layout_alignParentLeft="true"
                android:background="@drawable/screen" />
         </RelativeLayout>

And this is the java code

    private final class MyTouchListener implements OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {
          if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
          } else {
          return false;
          }
        }
      } 
     class MyDragListener implements OnDragListener {

        @Override
        public boolean onDrag(View v, DragEvent event) {
          int action = event.getAction();
          switch (event.getAction()) {
          case DragEvent.ACTION_DRAG_STARTED:
          // do nothing
            break;
          case DragEvent.ACTION_DRAG_ENTERED:

            break;
          case DragEvent.ACTION_DRAG_EXITED:        

            break;
          case DragEvent.ACTION_DROP:
            // Dropped, reassign View to ViewGroup
           if( v instanceof ViewGroup  && ((ViewGroup)v).getChildCount()!=0 ){
            View view = (View) event.getLocalState();
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            RelativeLayout container = (RelativeLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);}
            break;
          case DragEvent.ACTION_DRAG_ENDED:

            default:
            break;
          }
          return true;
        }
      }

And this is how i have applied touch and drag listener:

findViewById(R.id.screen).setOnTouchListener(new MyTouchListener());
findViewById(R.id.middle1).setOnDragListener(new MyDragListener());

Please have a look and suggest me how to solve it. Thanks in advance.

Rethinavel
  • 3,912
  • 7
  • 28
  • 49
Vaishali Sharma
  • 602
  • 3
  • 6
  • 24

1 Answers1

-1

use this MotionEvent.ACTION_MOVE am not sure edit text working or not but in my case images working fine see here is complete code

Android Drag and drop images on the Screen?

  public boolean onTouch(View v, MotionEvent event) {
   layoutParams = (RelativeLayout.LayoutParams) ima1.getLayoutParams();

     switch(event.getAction())                   
        {
          case MotionEvent.ACTION_DOWN:                          
                break;     

          case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();

          System.out.println("value of x" +x_cord);
          System.out.println("value of y" +y_cord);           

                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                   }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                   }
         layoutParams.leftMargin = x_cord-25;
         layoutParams.topMargin = y_cord-25;
         //   layoutParams.rightMargin = x_cord-25;
         //   layoutParams.bottomMargin = y_cord-25;
         ima1.setLayoutParams(layoutParams);
                 break;
           default: break;
          }  
           return true;
        }
     });
Community
  • 1
  • 1
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98