How can I get the available height of the screen in Android? I need to the height minus the status bar / menu bar or any other decorations that might be on screen and I need it
final View view = findViewById(R.id.root);
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// calculate the height in here...
ViewTreeObserver vto = view.getViewTreeObserver();
vto.removeGlobalOnLayoutListener(this);
}
});
Use this listener inside onCreate
(available since api 1). You will need to assign the id @+id/root
to the parent in your xml layout. There is no way the size can return a result of 0, since this listener makes a callback whenever the layouts has been positioned in view.