1

I have a relative layout in which i have a TextView having some text "Hi ! This is john". I want to move the text to any position i want on the screen, on touching the text.I am using onTouchListener() on the Text View.Can anybody please help me.

Lakshmipathi
  • 87
  • 2
  • 9

2 Answers2

0

Follow that carefully, very how set animation? Android Working with XML Animations then visit that android: move a view on touch move (ACTION_MOVE)

Community
  • 1
  • 1
Engr Waseem Arain
  • 1,163
  • 1
  • 17
  • 36
0

Set onTouch listner on your TextView and according to the position of it Move it to another location you want on MotionEvent.ACTION_UP event..

private final static int START_DRAGGING = 0;
    private final static int STOP_DRAGGING = 1;   
    private int status;
    int flag=0;
    float xAxis = 0f;
    float yAxis = 0f;
    float lastXAxis = 0f;
    float lastYAxis = 0f;
...     
@Override
public boolean onTouch(View v, MotionEvent me){
          if(me.getAction()==MotionEvent.ACTION_DOWN){
          status = START_DRAGGING;
          final float x = me.getX();
          final float y = me.getY();
          lastXAxis = x;
          lastYAxis = y;
          v.setVisibility(View.INVISIBLE);
          }else if(me.getAction()==MotionEvent.ACTION_UP){
          status = STOP_DRAGGING;
          flag=0;
          v.setVisibility(View.VISIBLE);
          }else if(me.getAction()==MotionEvent.ACTION_MOVE){
          if (status == START_DRAGGING){
          flag=1;
          v.setVisibility(View.VISIBLE);
          final float x = me.getX();
          final float y = me.getY();
          final float dx = x - lastXAxis;
          final float dy = y - lastYAxis;
          xAxis += dx;
          yAxis += dy;
          v.setX((int)xAxis);
          v.setY((int)yAxis);
          v.invalidate();
          }
        }
        return false;
      }
Akshay
  • 6,029
  • 7
  • 40
  • 59