It seems to me that there is a problem in the Android coordinate system. When I have a normal view (no request FEATURE_NO_TITLE), I then retrieve it int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();. This gives me 76px: 38pxfor the status bar and 38pxfor the title.
However , if I request FEATURE_NO_TITLEand then repeat the procedure, it getTop()returns 0px, even though the status bar is still visible!
This discrepancy should not change, because we usually do not care where the viewing of the content begins. However, it matters to me, because I position the view on the decor, which covers the entire visible window.
I know that this is not a header trick and device density, because if I request a custom title bar and give it a height 0, then it getTop()returns 38px.
The solution / workaround is to add 38 pixels manually when requested FEATURE_NO_TITLE. My question is: is this an Android bug? Or is there something that I don’t understand about how layouts work that could make this behavior understandable?
Thanks in advance!
Here is a minimal program that reproduces the problem. Run it twice and uncomment the specified line. I am going against the Android SDK 7 and working on the 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>
The 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);
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() {
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight = rectgle.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);
}
});
}
}
References