I would suggest next code:
Rect rect = new Rect();
Window window = activity.getWindow();
if (window != null) {
window.getDecorView().getWindowVisibleDisplayFrame(rect);
android.view.View v = window.findViewById(Window.ID_ANDROID_CONTENT);
android.view.Display display = ((android.view.WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
//return result title bar height
return display.getHeight() - v.getBottom() + rect.top;
}
Two examples:
1) Device 1
brand: samsung,
device: maguro,
board: tuna,
cpu_abi: armeabi-v7a,
display: ICL53F.M420KREL08,
manufacturer: samsung,
model: Galaxy Nexus,
ver.release: 4.0.2,
ver.sdk: 14;
Screen resolution: 1280 x 720.There are no hardware buttons on this device.
Results:
rect: left=0 right=720 top=72 bottom=1208;
v: left=0 right=720 top=72 bottom=1208;
display: height=1208 width=720;
correct result=72;
Device 1 has title bar at the top of the screen and status bar with software buttons at the bottom of the screen.
2) Device 2
device: bravo,
board: bravo,
cpu_abi: armeabi-v7a,
display: FRG83G,
manufacturer: HTC,
model: HTC Desire,
ver.release: 2.2.2,
ver.sdk: 8,
Screen resolution: 800 x 400. This device has hardware buttons.
Results:
rect: left=0 right=480 top=38 bottom=800;
v: left=0 right=480 top=0 bottom=800;
display: height=800 width=480;
correct result: phone_bar_height=38;
Device 2 has title bar at the top of the screen and hasn't status bar at all.
Two solutions were suggested above:
A) v.getTop() - rect.top
(it is incorrect for device 1 - it gives 0 instead of 72)
B) display.getHeight() - v.getHeight()
(it is incorrect for device 2 - it gives 0 instead of 38)
Variant:
display.getHeight() - v.getBottom() + rect.top
gives correct results in both cases.
Update
3) One more example (third device):
brand: LGE,
device: v909,
cpu_abi: armeabi-v7a,
display: HMJ37,
model: LG-V909,
ver.release: 3.1,
ver.sdk: 12
rect: left=0 right=1280 top=0 bottom=720
v: left=0 right=1280 top=0 bottom=720
Display: height=768 width=1280
phone_bar_height=48
Device 3 has horizontal orientation, hasn't title bar at the top of the screen and has status bar at the bottom of the screen.
So, here:
int size_of_title_bar = rect.top;
int size_of_status_bar = display.getHeight() - v.getBottom();
It's correct for devices 2 and 3. I am not sure about device 1. User sent me screenshot of device 1. There is a status bar with software button there. But expression "display.getHeight() - v.getBottom()" gives 0.