pouzzler
pouzzler

Reputation: 1834

RelativeLayout - dragging custom view

I have a custom view with an onTouch() listener. This view is added to a RelativeLayout so:

CustomView mv = new CustomView(mActivity);
RelativeLayout rl = ((RelativeLayout) mActivity.findViewById(R.id.main_layout));
rl.addView(mv);

I wish the view to be dragged with the finger under API8, preferably by code residing in the view's own onTouch() listener.

It's probably a few lines of code along the edge of setPadding() or RelativeLayout.LayoutParams(), but after one hour of trial and error, I can't find it.

Any clues? Thanks a lot.

PS: The onTouch works like it should, I just don't know what lines of code I could add to it that would move the view.

Upvotes: 1

Views: 158

Answers (1)

Tomas Vondracek
Tomas Vondracek

Reputation: 143

I would suggest to use FrameLayout and then change margin of your CustomView like this:

LayoutParams params = (FrameLayout.LayoutParams) mv.getLayoutParams();
params.leftMargin = x;
params.topMargin = y;
mv.setLayoutParams(params);

Hope this helps.

Upvotes: 1

Related Questions