It appears to me that there is a problem in Android's coordinate system. When I have a normal view (without requesting FEATURE_NO_TITLE
), I then retrieve int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
. This gives me 76px
: 38px
for the status bar, and 38px
for the title bar.
However, if I request FEATURE_NO_TITLE
and then repeat the procedure, getTop()
returns 0px
, despite the fact that the status bar is still visible!
This discrepancy shouldn't make a difference, because we don't usually care where the content view starts. However, it does matter to me because I position views on the decor view -- which covers the entire visible window.
I know that this is not a trick of the titlebar and the density of the device, because if I request a custom titlebar, and give it 0
height, then getTop()
returns 38px
.
The solution/workaround is to add 38 pixels manually when requesting FEATURE_NO_TITLE
. My question is: is this an Android bug? Or is there something I'm not understanding about how layouts work that would make this behavior understandable?
Thanks in advance!
Here is a minimal program that reproduces the problem. Run it twice, and uncomment the indicated line. I am compiling against Android SDK 7, and running on a Samsung Galaxy S with Android 2.3.4.
Layout: main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutParent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/someId"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</RelativeLayout>
Code: TestStatusBarActivity.java
package com.test.teststatusbar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.widget.RelativeLayout;
public class TestStatusBarActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Uncomment the following line to see the alternate behavior
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
RelativeLayout layoutParent = (RelativeLayout) findViewById(R.id.layoutParent);
View something = findViewById(R.id.someId);
something.setBackgroundColor(Color.CYAN);
ViewTreeObserver vto = layoutParent.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// decor window top
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight = rectgle.top;
// "content view" top
int contentViewTop = window.findViewById(
Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight = contentViewTop - StatusBarHeight;
Log.i("STATUSBARTEST", "StatusBar Height = " + StatusBarHeight
+ " , TitleBar Height = " + TitleBarHeight
+ ", Content top = " + contentViewTop);
}
});
}
}