I have the following XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/rl"
android:background="@color/red"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="10dp"
android:layout_height="10dp" />
</RelativeLayout>
When I call:
view.layout(50, 50, 1024 - 50, 600 - 50);
from onCreate() and onStart(), it has no effect. But when I add the same call to shouldOverrideKeyEvent() and press a key, it works. I think it’s because my settings are being overriden by XML’s ones. So, the question is: when should I call layout() as soon as possible, but after android:layout_width=”10dp” and android:layout_height=”10dp” are already applied.
UPDATE
I use the solution by blessenm.
private class LayoutListener implements OnGlobalLayoutListener
{
public void onGlobalLayout()
{
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
// Workaround for a 'fill_parent' bug on my tablet (Kindle Fire).
_WebView.layout(0, 0, screenWidth, screenHeight);
_WebView.loadUrl("http://192.168.1.2");
//_Layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
If I comment removeGlobalOnLayoutListener() out, it stops working. Debugger shows it’s called twice, so I guess the second call only actually sets the size. So, I’ve decided not to remove the listener (if it’s called 2 times, that’s not a performance killer). It’d seemed OK, until I realized it’s called whenever I click a permanent bottom-bar, causing toolbar appearance. And since I load a site after setting layout, it’s being reloaded every time. And I have to load the site only when the view’ size is correct, because a server sends content, specially optimized for the given size.
That’s why I cannot leave the listener and should remove it. But when? Just count to 2? Will it work on every device/Android version? Why does it work for the second call, but not for the first?
Regards,
I think its because in the onCreate event the layouts haven’t been drawn or measured yet. So If you try to call layout in that event it wont have an impact. Try to put your code in the viewtreeobserver, it might work.
Add this to your activity’s onCreate
where layout can be your parent or outer layout.