shyam
shyam

Reputation: 1306

move text on touch motion event android

i have text View in layout and set text in layout..now i want to maove this text on touch event means depends on the motion event ..so how to move this text and set text , where i put off my finger from screen..i searched in Google and found , can get x and y cordinates of screen using ,event.getX() and ,y= + event.getY()..but how to use it to set the text at that position only..

   TextView textView = (TextView)findViewById(R.id.textView);

    // this is the view on which you will listen for touch events
    View touchView = findViewById(R.id.touchView);
    touchView.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

 }}

  }
thanks 

Upvotes: 1

Views: 2951

Answers (3)

SANJAY GUPTA
SANJAY GUPTA

Reputation: 1604

Use following code to move text on finger move within layout.

write_text.setOnTouchListener(new OnTouchListener() {
            @SuppressLint("NewApi")
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    break;

                case MotionEvent.ACTION_MOVE:
                    v.setX(event.getRawX() - write_text.getWidth() / 2.0f);
                    v.setY(event.getRawY() - write_text.getHeight() / 2.0f);
                }
                return true;
            }
        });

Upvotes: 0

Mudasar
Mudasar

Reputation: 249

your question is not clear what do you mean by moving text on touch ? you want to move the text where finger touches ? if you want this then make two variables x and y in activity and update them with like this.

  x = event.getX()  // or getRawX();
 y = event.getY();

Upvotes: 4

rockeye
rockeye

Reputation: 2773

Depending on the layout you use, it should look like this :

FrameLayout.LayoutParams lLayoutParam = (FrameLayout.LayoutParams) your_view.getLayoutParams();
lLayoutParam.leftMargin = x;
lLayoutParam.topMargin = y;
your_FrameLayout.updateViewLayout(your_view, lLayoutParam);

I am not an expert in Android UI, but, as far as I known, FrameLayout seems to be appropriate to control the position with absolute values.

Upvotes: 0

Related Questions