1

Im trying to get my TextField to move on the screen. I want to be able to drag it to a new position on the screen. I´ve been struggling with this for days now and I really cant figure this one out... This is what I´ve done so far:

InputProcessor drag = new InputAdapter() {

                    @Override
                    public boolean touchDown(int screenX, int screenY, int pointer,
                                    int button) {
                            // TODO Auto-generated method stub
                            return super.touchDown(screenX, screenY, pointer, button);
                    }

                    @Override
                    public boolean touchUp(int screenX, int screenY, int pointer,
                                    int button) {
                            int x = Gdx.input.getX();
                            int y = Gdx.input.getY();

                            textField.setPosition(x, y);

                            return true;
                    }

                    @Override
                    public boolean touchDragged(int screenX, int screenY, int pointer) {
                            // TODO Auto-generated method stub
                            return super.touchDragged(screenX, screenY, pointer);
                    }


            };
            game.inputMultiplexer = new InputMultiplexer();


            game.inputMultiplexer.addProcessor(stage);
            game.inputMultiplexer.addProcessor(stagePurc);
            game.inputMultiplexer.addProcessor(stageText);
            game.inputMultiplexer.addProcessor(drag);

            Gdx.input.setInputProcessor(game.inputMultiplexer);

Here´s the textfield:

 final TextField textField = new TextField(prefs.getString("textField", "Enter name:"), textstyle);

    textField.setX(textX);
    textField.setY(textY);
    textField.setMaxLength(20);
    textField.setWidth(textWidth);
    textField.setHeight(textHeight);
karl
  • 1,766
  • 2
  • 13
  • 24
Benni
  • 969
  • 1
  • 19
  • 29

1 Answers1

0

Put the TextView in a LinearLayout:

 <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:clipChildren="false"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/text_color"
            android:textSize="16sp" />

 </LinearLayout>

Then set a touch listener:

txt.setOnTouchListener(new View.OnTouchListener() {
            int initialX = 0;
            int initialY = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = (int) event.getX();
                    initialY = (int) event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                            int currentX = (int) event.getX();
                            int currentY = (int) event.getY();
                            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) txt.getLayoutParams();

                            int left = lp.leftMargin + (currentX - initialX);
                            int top = lp.topMargin + (currentY - initialY);
                            int right = lp.rightMargin - (currentX - initialX);
                            int bottom = lp.bottomMargin - (currentY - initialY);

                            lp.rightMargin = right;
                            lp.leftMargin = left;
                            lp.bottomMargin = bottom;
                            lp.topMargin = top;

                            txt.setLayoutParams(lp);
                    break;
                default:
                    break;
                }
                return true;
            }
    });

Works pretty well. I got this code from here: How to make the TextView drag in LinearLayout smooth, in android?

Community
  • 1
  • 1
Mr. Kevin Thomas
  • 837
  • 2
  • 13
  • 21