Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the TextView drag in LinearLayout smooth, in android?

I have a situation which I am not able to fix, hopefully I will get some advises from you.

The situation is simple: I have a LinearLayout in which I have a TextView with multiple lines of text. The user is able to drag the TextView around until he finds a position he likes. What is really important is that the TextView can be partially out of the LinearLayout (it will appear cut).

Here are some code samples:

 <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>

As you can see the LinearLayout has clipChildren = false to allow cutoff of text. For the textview I 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;
            }
        });

And here is my problem: As you can see I've set all the layout parameters (right, left, bottom, top margins). Why is that ?

1) If I use only left/top the drag is smooth enought but text get's wrapped at the right border instead of getting cut. Probably due to right 0 margin value.

2) If I use all margins, the text gets cut as I want, but the movement is not smooth, it just jumps around for a few pixels.

How can I fix this ?

like image 628
Alin Avatar asked Feb 04 '13 09:02

Alin


1 Answers

Well. this is weird. By inserting the TextView inside another LinearLayout with the same size as the initial one, makes everything smooth. I have no idea why.

like image 102
Alin Avatar answered Sep 22 '22 15:09

Alin