Strange behavior when using WebView and RelativeLayout in full screen mode

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

-

I have an activity that consists of a web view and a button below. I set the action window to full screen 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, web browsing seems to “press” a button on the screen. Here is a screenshot:

wrong

( larger version )

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

correct

( larger version )

Here is the layout I am using (note that in my application the top layout should be 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 the system settings, the problem will disappear (although there it is a failure when the whole view has a top edge and then it returns to the correct position).

Has anyone encountered a similar problem and knew how to fix it?

+7
source share
8 answers
Finally, I found the answer. Adding the WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS flag to the window solves my problem.

See this post: flag_fullscreen + windowNotitle == button focus error .

+2
source

I just added a button to Hello WebView and everything looks right. Then I converted main.xml from LinearLayout to RelativeLayout , and everything still looks right. I think you are making it too complicated. :-) Try to remove most of this initialization code from onCreate() and just specify it in 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> 
+1
source

Try aligning the web view with the parent top and align the button to the bottom of the parent and be below the web view. I found a time when, for some reason, the elements do not line up correctly, if they are not all stretched in a linear sequence, either tied at the top or bottom of the screen.

I assume that you do not override the onMeasure () method and change the measured height of the button. I created many (too many) errors when developing my applications, calculating the height of the element during the layout phase and spent hours struggling with debugging the layout definition code only to find that it was good, but calculating the height was feeding incorrect data to layout rendering phase.

+1
source

If the value of RelativeLayout absolutely necessary, you can change it to LinearLayout :

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <WebView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="I'm a button"/> </LinearLayout> 

This should give a better result.

0
source

To hide the title bar, I use:

 getWindow().requestFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

Tested on Galaxy S 2 (Android 2.3.3) and Huawei U8180 X1 (Android 2.2.2) and did a great job with your relative layout.

The screenshot gives the impression that the problem is rather that you requested an incomplete progress indicator. The fact that the problem disappears when turning off the animation also supports this theory.

0
source

You can try this. The top layout is still RelativeLayout , the only difference with yours is LinearLayout completes everything:

  <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" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:weightSum="1"> <WebView android:id="@+id/web_view" android:layout_width="fill_parent" android:layout_height="0px" android:layout_weight=".90" /> </LinearLayout> </RelativeLayout> 

In LinearLayout , WebView occupies 90% entire window (and parent), and the remaining button is 10% . The height is set to 0px for both, so the weight can do the job for both of them. The cost of parsing this xml is small.

And in the Java code, I did:

  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); LinearLayout mLinearLayout = (LinearLayout)findViewById(R.id.ll); Button mButton = new Button(this); LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 0.10f); mButton.setText("Click me!"); mButton.setLayoutParams(mParams); mLinearLayout.addView(mButton); mLinearLayout.invalidate(); } 
0
source

Please refer to this link:

http://marakana.com/forums/android/examples/58.html

Download the project file (.zip).

Import it and let me know if this project helps.

0
source

below code can help you

  webView = (WebView) findViewById(R.id.webview1); webView.setInitialScale(100); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { // TODO Auto-generated method stub super.onPageFinished(view, url); view.scrollTo(0, 0); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadDataWithBaseURL(null, yourdata, "text/html", "utf-8", null); // view.loadUrl(url); return true; } }); webView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { try { //your stuff } catch (Throwable e) { e.printStackTrace(); } } } 
0
source

All Articles