0

I have a FrameLayout in which I place a View with Gravity.CENTER_VERTICAL. I need to get the left and top position of this View, relative to the FrameLayout. I tried with myView.top but it gives 0, though this is not correct. So, how can I get the position of FrameLayout childs?

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • Is the child perhaps filling the parent, so `0` would be correct? – CommonsWare May 19 '20 at 20:20
  • no, it's only filling it horizontally, vertically it's not as high as the FrameLayout. – stefan.at.kotlin May 19 '20 at 20:27
  • 2
    Are you trying to get that value too soon, before it's been laid out? Please provide a [mcve]. – Mike M. May 19 '20 at 20:31
  • hmm I meanwhile found it that at the time I query this, the FrameLayout is not yet laied out. – stefan.at.kotlin May 19 '20 at 20:31
  • @MikeM. yeah, exactly. How can I force the layout? Is there any callback for finished layout that I can hook into? – stefan.at.kotlin May 19 '20 at 20:31
  • Kinda, but I find it pretty ugly. It's called `OnGlobalLayoutListener`, I believe. I usually find it's sufficient, and a little cleaner, to `post()` a `Runnable` to the root `View` that's involved. Both options, and others, are covered in [this answer](https://stackoverflow.com/a/24035591). – Mike M. May 19 '20 at 20:36
  • thank you, will have a look at these possibilities. You should post the link as answer :-) – stefan.at.kotlin May 19 '20 at 20:44
  • Nah, links aren't appropriate answers on Stack Overflow, and I certainly don't want to take credit for someone else's work. :-) I would mark this as a duplicate instead, but, to be honest, this is such a common issue that we already have more than enough other posts linking the original, so if one of those suggestions ends up working for you, please feel free to delete this, if you like. Otherwise, just let me know, and I can mark it, or help you further, if those suggestions don't work. Cheers! – Mike M. May 19 '20 at 20:50
  • Note that if you are using Kotlin, you probably have access to `doOnLayout()` and `doOnNextLayout()` extension functions, which make this task a lot simpler. – CommonsWare May 19 '20 at 21:34

0 Answers0