I created my own component that extends RelativeLayout , inside it I have a field of type View that fits with the fields:

The blue box represents my parental view, and the red circle represents the child.
Now I want to allow the user to move this red circle by moving my finger across the screen. This is my onTouchEvent() method:
@Override public boolean onTouchEvent(MotionEvent event) { int eventAction = event.getAction(); switch (eventAction) { case MotionEvent.ACTION_DOWN:
But I quickly found that he does not update his position while the user moves his finger across the screen, but instead he does this only when the user releases his finger from the screen. Thus, basically, when I move my finger, the circle remains in one position, but when I stop moving, the circle is drawn in a new position. I tried to use the invalidate() method for both parent and child view, but that didn't help. I guess this may have something to do with the UI thread, but I don't have access to runOnUiThread()' method as it not an Activity`.
EDIT: I passed my activity to the class to use runOnUiThread() . Now my case: ACTION_MOVE looks like this:
case MotionEvent.ACTION_MOVE: marginX = (int)event.getX(); marginY = (int)event.getY(); activity.runOnUiThread(new Runnable() { public void run() { RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)pointer.getLayoutParams(); lp.setMargins(marginX, marginY, 0, 0); pointer.invalidate(); } }); break;
source share