I understand that I cannot get the dimensions too early before the UI has been set up.
I know I have to get the dimensions after the onCreate().
But even if I do the following, they still always return me 0.
onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_path);
...
onCreateDone = true;
}
I hope to get the dimensions in onSensorChanged()
// called when sensor values change
public void onSensorChanged(SensorEvent event) { // is roughly called 350 times in 1s
if (layoutSizeNotObtained && onCreateDone) { // only runs once to get the layout size
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.activity_show_path_relativelayout);
int layoutWidth = relativeLayout.getWidth();
int layoutHeight = relativeLayout.getHeight();
Log.d("ShowPathActivity", "layout size: " + layoutWidth + " " + layoutHeight);
Constant.setInitialX(layoutWidth / 2);
Constant.setInitialY(layoutHeight / 2);
layoutSizeNotObtained = false;
}
....
}
Where goes wrong?