2

I have got a textView or small Icon and I want to drag it only vertical.

What I tried: Create an own ShadowBuilder but make him invisible. So others views get called over the OnDragListener but the ShadowView is not visible.

Instead I display an own view which is moved like here: How to make the TextView drag in LinearLayout smooth, in android? with the different, only in vertical direction. The Problem is, the onTouchListener is only called twice if dragging is activated. So no moving could be done.

That solution does not work for me I think. Drag an image only horizontally or vertically in android?

Maybe any idea? Thanks for reading.

Community
  • 1
  • 1
Viatorus
  • 1,804
  • 1
  • 18
  • 41

2 Answers2

1

try out my solution here, but only change the top&bottom margins instead.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
1
  findViewById(R.id.btn_submit).setOnTouchListener(new View.OnTouchListener()
        {
            int prevX,prevY;

            @Override
            public boolean onTouch(final View v,final MotionEvent event)
            {
                final LinearLayout.LayoutParams par=(LinearLayout.LayoutParams)v.getLayoutParams();
                switch(event.getAction())
                {
                    case MotionEvent.ACTION_MOVE:
                    {
                        par.topMargin+=(int)event.getRawY()-prevY;
                        prevY=(int)event.getRawY();

                        prevX=(int)event.getRawX();
                        v.setLayoutParams(par);
                        return true;
                    }
                    case MotionEvent.ACTION_UP:
                    {
                        par.topMargin+=(int)event.getRawY()-prevY;

                        v.setLayoutParams(par);
                        return true;
                    }
                    case MotionEvent.ACTION_DOWN:
                    {
                        prevX=(int)event.getRawX();
                        prevY=(int)event.getRawY();
                        par.bottomMargin=-2*v.getHeight();

                        v.setLayoutParams(par);
                        return true;
                    }
                }
                return false;
            }
        });
Rajesh N
  • 6,198
  • 2
  • 47
  • 58