Get status bar height dynamically from XML

I have already found the answer:

The recommended way is to use the WindowInsets API, like the following:

view.setOnApplyWindowInsetsListener { v, insets -> 
    view.height = insets.systemWindowInsetTop // status bar height
    insets
}

Accessing the status_bar_height, as it's a private resource and thus subject to change in future versions of Android, is highly discouraged.


Method provides the height of status bar

public int getStatusBarHeight() {
  int result = 0;
  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
 if (resourceId > 0) {
  result = getResources().getDimensionPixelSize(resourceId);
}   
 return result;
}

It is not a solution, but it can explain you how it should be don in correct way and how things are going under the hood: https://www.youtube.com/watch?v=_mGDMVRO3iE

OLD SOLUTION:

Here is small trick:

@*android:dimen/status_bar_height

android studio will show you an error, but it will work like a charm :)