Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour when using WebView and RelativeLayout in fullscreen

EDIT: Here's the video showing the problem: www.youtube.com/watch?v=5ZZsuMH5T9k I'm really stuck here :/

--

I have an activity which consists of a webview and a button at the bottom. I set the window of the activity to fullscreen using this code in onCreate:

@Override
public void onCreate(Bundle savedInstanceState) {
  InstaFetchApplication.applyCurrentTheme(this);

  super.onCreate(savedInstanceState);

  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  requestWindowFeature(Window.FEATURE_PROGRESS);

  if (UserPreferences.getIsFullScreenReadingEnabled(this)) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // this just do this: window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    ActivityUtils.toggleFullScreen(this, true);
  }

  setContentView(R.layout.view_article);

  // ...
}

The problem is that most of the time the webview seems to "push" the button off the screen. Here's the screenshot:

wrong

(larger version)

What I would expect to see (and sometimes I do) is here:

correct

(larger version)

Here's the layout I'm using (note that in my app the top layout has to be a RelativeLayout):

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/layout_test"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>
  <Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Click me!"
  />
  <WebView
    android:id="@+id/web_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@id/button"
  />
</RelativeLayout>

I also noticed that if you turn off all animations on the device using system settings, the issue is gone (though there's this glitch when the whole view has a top margin and then it jumps back in the correct position).

Did anyone encounter similar problem and know how to fix it?

like image 364
Marek Stój Avatar asked Dec 18 '11 08:12

Marek Stój


2 Answers

Finally I found the answer. Adding WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS flag to the window solved my problem.

See this post: flag_fullscreen + windowNoTitle == button focus bug.

like image 66
Marek Stój Avatar answered Oct 01 '22 03:10

Marek Stój


I just added a button to the Hello WebView tutorial, and everything seems to look right. Then I converted main.xml from LinearLayout to RelativeLayout, and everything still seems to look right. I think you are making things too complicated. :-) Try removing most of that initialization code from onCreate() and just specify it in the XML.

Here is my project.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.hellowebview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HelloWebViewActivity"
            android:label="@string/app_name" 
            android:theme="@android:style/Theme.NoTitleBar"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

HelloWebViewActivity.java:

package com.example.android.hellowebview;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HelloWebViewActivity extends Activity {

    WebView mWebView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_rl);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new HelloWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

main_ll.xml (LinearLayout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
    />
    <Button
        android:id="@+id/mainbtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="@string/btn_label"
    />
</LinearLayout>

main_rl.xml (RelativeLayout):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/mainbtn"
    />
    <Button
        android:id="@+id/mainbtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/btn_label"
    />
</RelativeLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, HelloWebViewActivity!</string>
    <string name="app_name">HelloWebView</string>
    <string name="btn_label">Push Me</string>

</resources>
like image 38
Sparky Avatar answered Oct 01 '22 04:10

Sparky