Ungureanu Liviu
Ungureanu Liviu

Reputation: 4124

How can I get the position of a view in an AbsoluteLayout?

Can anyone give me a tip for how to get the position of a view what is a child of an AbsoluteLayout? I want to do this for drag and drop the selected view.

Thank you!

Upvotes: 1

Views: 1055

Answers (2)

Romain Guy
Romain Guy

Reputation: 98501

To know where a child is in its parent, simply call getLeft() and getTop(). Also, do not use AbsoluteLayout :)

Upvotes: 2

Ramps
Ramps

Reputation: 5298

AbsoluteLayout is deprecated, so probably you would have to provide your own drag&drop layout by extending ViewGroup. In general, layout it is responsible for positioning children widgets. This is done in onLayout() method which you would have to override. It will be probably something like this:


protected void onLayout(boolean changed, int l, int t, int r, int b) {
   final int count = getChildCount();
   for(int i=0; i<count; i++){
      final View child = getChildAt(i);
      if(GONE != child.getVisibility()){
        //position child
        child.layout(left, top, right, bottom);
      }
   }
}

So, by implementing your own DragAndDropLayout - you know the position of your children. But, maybe there is simplier solution.
Regards!

Upvotes: 2

Related Questions