0

I have this activity where in I need to do some layout at runtime (in this particular instance I need to set the indicator bounds (ExpandableListView.setIndicatorBound) so that the group indicator aligns to the right side of the view instead of the default left.

I've got this code in my Activity:onCreate:

int viewWidth = mListView.getWidth();
int groupIndicatorWidth = getResources().getDrawable(R.drawable.up).getMinimumWidth(); 
mListView.setIndicatorBounds(viewWidth - groupIndicatorWidth, viewWidth);
app.fetchItems(this);

Quite understandably, viewWidth turns out to be 0 at runtime.

My question is what method should I implement/overload when the view's children would have been laid out and their dimensions valid? Basically on Windows we would generally put such code in OnSize/OnResize.

I found Activity:onPostCreate and Activity:onPostResume, but I also found this in the docs - "Applications will generally not implement this method; it is intended for system classes to do final initialization after application code has run."

Edit

Found this thread which has several workarounds listed: Android: Need to use onSizeChanged for View.getWidth/Height() in class extending Activity

Community
  • 1
  • 1
Code Poet
  • 11,227
  • 19
  • 64
  • 97

1 Answers1

0

The most obvious way to know, when the size of the view is changed (ListView in your example), is to extend that view and override its onSizeChanged(int w, int h, int oldw, int oldh) method.

a.ch.
  • 8,285
  • 5
  • 40
  • 53