-1

I have an Android application that create all the layout programmatically in the onCreate method, after that I need to do some calculation on the coordinates of some of the views I created, but when I try to get coordinates on screen, it always return 0,0. I think that may be caused from the fact that the activity is still not rendered as in fact by debugging I still see a grey screen if i place a breakpoint on the point where I get coordinates, how can I calculate coordinates after starting activity? For me it's ok even if it's out of onCreate, I just need it to be executed after that the activity rendering, and if possible I'd like to not use a OnGlobalLayoutListener (if there's no other way well... i'll just get used to the idea, but i'd like to know if there's some other way). Ah, I even tried to put it on the onResume, but it doesn't work, probably when onResume is called the rendering is still not finished.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Celeste Capece
  • 1,306
  • 2
  • 11
  • 20
  • You're likely trying to access components of the layout before they are inflated. We can't really know unless you post some code. At least show us the onCreate stuff. – NoChinDeluxe Oct 05 '15 at 15:55

1 Answers1

1

Extends View class and writing your own protected void onSizeChanged(int w, int h, int oldw, int oldh) { could help you.

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    final float yCenterX = w / 2;
    final float yCenterY = h / 2;
    ...
}
AndreyICE
  • 3,574
  • 29
  • 27