Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Update 2014-11-13:</strong> Since Android KitKat neither of the solutions described below are working -- you will need to look for different approaches like e.g. <a href="https://github.com/ManuelPeinado/FadingActionBar" rel="noreferrer">Manuel Peinado's FadingActionBar</a> which provides a scrolling header for WebViews.</p> <p><strong>Update 2012-07-08:</strong> "Nobu games" kindly created a <a href="http://code.google.com/p/android-titlebar-webview/" rel="noreferrer"><code>TitleBarWebView</code></a> class bringing the expected behavior back to Android Jelly Bean. When used on older platforms it will use the hidden <code>setEmbeddedTitleBar()</code> method and when used on Jelly Bean or above it will mimic the same behavior. The source code is available under the Apache 2 license <a href="http://code.google.com/p/android-titlebar-webview/" rel="noreferrer">at google code</a></p> <p><strong>Update 2012-06-30:</strong> It seems as if the <code>setEmbeddedTitleBar()</code> method has been removed in Android 4.1 aka Jelly Bean :-(</p> <p><strong>Original answer:</strong></p> <p>It <em>is</em> possible to place a <code>WebView</code> into a <code>ScrollView</code> and it does work. I am using this in <a href="https://play.google.com/store/apps/details?id=com.gettingmobile.goodnews" rel="noreferrer">GoodNews</a> on Android 1.6 devices. The main drawback is that the user cannot scroll "diagonal" meaning: If the web content exceeds the width of the screen the <code>ScrollView</code> is responsible for vertical scrolling at the <code>WebView</code> for horizontal scrolling. As only one of them handles the touch events you can either scroll horizontally <em>or</em> vertically but not diagonal. </p> <p>Further on there are some annoying problems as described by you (e.g. empty vertical space when loading a content smaller than the previous one). I've found workarounds for all of them in GoodNews, but cannot remember them now, because I've found a much better solution:</p> <p>If you only put the <code>WebView</code> into the <code>ScrollView</code> to place Controls <em>above</em> the web content and you are OK to support only Android 2 and above, then you can use the hidden internal <code>setEmbeddedTitleBar()</code> method of the <code>WebView</code>. It has been introduced in API level 5 and (accidentally?) became public for exactly one release (I think it was 3.0). </p> <p>This method allows you to embed a layout into the <code>WebView</code> which will be placed above the web content. This layout will scroll out the screen when scrolling vertically but will be kept at the same horizontal position when the web content is scrolled horizontally.</p> <p>As this method isn't exported by the API you need to use Java reflections to call it. I suggest to derive a new class as followed:</p> <pre><code>public final class WebViewWithTitle extends ExtendedWebView { private static final String LOG_TAG = "WebViewWithTitle"; private Method setEmbeddedTitleBarMethod = null; public WebViewWithTitle(Context context) { super(context); init(); } public WebViewWithTitle(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { try { setEmbeddedTitleBarMethod = WebView.class.getMethod("setEmbeddedTitleBar", View.class); } catch (Exception ex) { Log.e(LOG_TAG, "could not find setEmbeddedTitleBar", ex); } } public void setTitle(View view) { if (setEmbeddedTitleBarMethod != null) { try { setEmbeddedTitleBarMethod.invoke(this, view); } catch (Exception ex) { Log.e(LOG_TAG, "failed to call setEmbeddedTitleBar", ex); } } } public void setTitle(int resId) { setTitle(inflate(getContext(), resId, null)); } } </code></pre> <p>Then in your layout file you can include this using</p> <pre><code>&lt;com.mycompany.widget.WebViewWithTitle xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; </code></pre> <p>and somewhere else in your code you can call the <code>setTitle()</code> method with the ID of the layout to be embedded into the <code>WebView</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload