0

How do I keep track of the position of a UI element, say a TableLayout?

What I mean by this is, whenever its position changes, there is some kind of event handler gets called and then inside the event handler code I can get the coordinate of this TableLayout (maybe the coordinates of its upper left corner)

A sample code will be appreciated!

Thanks

Jiechao Wang
  • 922
  • 1
  • 15
  • 32

1 Answers1

0

Almost any widget or layout has onLayout method which receives as parameters left, top, right and bottom positions. Base android.view.View class even has a listener for this event onLayoutChangeListener, but TableLayout does not. So you can extend it and override onLayout method to do what you want.

vasart
  • 6,692
  • 38
  • 39
  • Thanks for your reply. But it seems that the overridden `onLayout` method does not get called when `TableLayout`'s absolute position changes. – Jiechao Wang Jun 30 '12 at 18:40
  • What if you override `onLayout` of `TableLayout`'s parent? – vasart Jun 30 '12 at 19:21
  • Thanks for this update. I tried it, but i didn't work. Then I realize that the parent of the `TableLayout` is `ScrollView`, so that I look at [here](http://stackoverflow.com/questions/3948934/synchronise-scrollview-scroll-positions-android) and [here](http://stackoverflow.com/questions/2638342/incorrect-coordinates-from-getlocationonscreen-getlocationinwindow) to constantly get the absolute position of the `TableLayout` when user scrolls `ScrollView`. – Jiechao Wang Jul 02 '12 at 18:26