The width and height of a Layout, when they are not explicitly defined, are given only after all children are drawn.
This is the code i use:
//layout = (RelativeLayout) findViewById(R.id.layout);
layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Ensure you call it only once :
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
width = layout.getWidth();// width must be declare as a field
height = layout.getHeight;// height must be declare as a field
levelCounter.setText("" + width);´
}
});
It must be in onCreate.