0
votes

can any one help me how to drag and drop the text view on entire image view. i found from this drag n drop textview in android here the text view is static, in my app textview and image view's are dynamically change. `@Override public boolean onTouchEvent(MotionEvent event) {

  switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN:
         // Remember our initial down event location.
         startX = event.getRawX();
         startY = event.getRawY();
         break;
    case MotionEvent.ACTION_MOVE:
         float x = event.getRawX();
         float y = event.getRawY();
        // Calculate move update. This will happen many times
        // during the course of a single movement gesture.
        scrollByX = x - startX; //move update x increment
        scrollByY = y - startY; //move update y increment
        startX = x; //reset initial values to latest
        startY = y;
        invalidate(); //force a redraw
        break;
      }
   return true; //done with this event so consume it
 } 

i wright this code, but it's not working properly.

1

1 Answers

2
votes

You can always change the image and text of existing view programatically using setText sort of methods.

There are many drag and drop tutrials if you google it. strongly suggest you try them out. Basically what you do is implement a OnTouchListener on the TextView like

textview.setOntouchListener(listen);

OnTouchListener onThumbTouch = new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {   
            switch(v.getId())
            {
                case R.id.slider_thumb_id:
                {
                    switch(event.getAction())
                    {
                        case MotionEvent.ACTION_MOVE:
                        {                           


                        }
                        case MotionEvent.ACTION_UP:
                        {                           

                        }
                    }
                    break;
                }
            }
            return true;
        }
    };

Here from the event attribute you get the value of x and y and the touch event and use that to alter the position of the textview. I did it by add add/subtracting the margin values.

EDIT: This is what I did to update the position of my view Look event.getRawY(); is going to give you the distance from the parent view. So if I get event.getRawY() = 50. I know I need the text view to be 50 pixels from the top.

LinearLayout.LayoutParams iconParams = (LinearLayout.LayoutParams) 
   v.getLayoutParams();
iconParams.topMargin = 50;
v.setLayoutParams(iconParams);

So now the view that i selected is 50 px from the top of the parent view.