Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to move components around the screen using the standard android apis?

I would like to produce an android user interface which allows the user to move added components/widgets around the screen by selecting them and then dragging them around.

Is this possible using the standard android apis?

like image 538
user85034 Avatar asked Mar 31 '09 08:03

user85034


1 Answers

Yes. It depends what you are trying to achieve.

It can be done using the standard APIs, but this functionality is not part of the standard APIs. That is, there is no widget.DragOverHere() method unless you write one.

That said, it would not be terribly complicated to do. At a minimum, you would need to write a custom subclass of View and implement two methods: onDraw(Canvas c) and onTouch(MotionEvent e). A rough sketch:

class MyView extends View {

    int x, y;  //the x-y coordinates of the icon (top-left corner)
    Bitmap bitmap; //the icon you are dragging around

    onDraw(Canvas c) {
      canvas.drawBitmap(x, y, bitmap);
    }

    onTouch(MotionEvent e) {
      switch(e.getAction()) {
      case MotionEvent.ACTION_DOWN:
        //maybe use a different bitmap to indicate 'selected'
        break;
      case MotionEvent.ACTION_MOVE:
        x = (int)e.getX();
        y = (int)e.getY();
        break;
      case MotionEvent.ACTION_UP:
        //switch back to 'unselected' bitmap
        break;
      }
      invalidate(); //redraw the view
    }
}
like image 109
allclaws Avatar answered Oct 18 '22 07:10

allclaws