Android WebView inside ScrollView scrolls only scrollview
Here is the solution. Found online. I have subclassed WebView and i'm using the requestDisallowInterceptTouchEvent(true);
method to allow my webview to handle the scroll event.
TouchyWebView.java
package com.mypackage.common.custom.android.widgets
public class TouchyWebView extends WebView {
public TouchyWebView(Context context) {
super(context);
}
public TouchyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TouchyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onTouchEvent(MotionEvent event){
requestDisallowInterceptTouchEvent(true);
return super.onTouchEvent(event);
}
}
And in layout.xml
<com.mypackage.common.custom.android.widgets.TouchyWebView
android:id="@+id/description_web"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Webview in Scrollview
Update 2014-11-13: Since Android KitKat neither of the solutions described below are working -- you will need to look for different approaches like e.g. Manuel Peinado's FadingActionBar which provides a scrolling header for WebViews.
Update 2012-07-08: "Nobu games" kindly created a TitleBarWebView
class bringing the expected behavior back to Android Jelly Bean. When used on older platforms it will use the hidden setEmbeddedTitleBar()
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 at google code
Update 2012-06-30: It seems as if the setEmbeddedTitleBar()
method has been removed in Android 4.1 aka Jelly Bean :-(
Original answer:
It is possible to place a WebView
into a ScrollView
and it does work. I am using this in GoodNews 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 ScrollView
is responsible for vertical scrolling at the WebView
for horizontal scrolling. As only one of them handles the touch events you can either scroll horizontally or vertically but not diagonal.
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:
If you only put the WebView
into the ScrollView
to place Controls above the web content and you are OK to support only Android 2 and above, then you can use the hidden internal setEmbeddedTitleBar()
method of the WebView
. It has been introduced in API level 5 and (accidentally?) became public for exactly one release (I think it was 3.0).
This method allows you to embed a layout into the WebView
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.
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:
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));
}
}
Then in your layout file you can include this using
<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"
/>
and somewhere else in your code you can call the setTitle()
method with the ID of the layout to be embedded into the WebView
.
I want to scroll the scrollview not the webview
Try to replace ScrollView
with NestedScrollView
and Add this line in onCreate view.setNestedScrollingEnabled(false);
Hope this help.
WebView inside NestedScrollView cannot be scrolled
Got this one working with changing the height attribute of the underlying Webview.
For NestedScrollView use attribute
android:fillViewport="true"
and for WebView use android:layout_height="wrap_content"
Full Answer here
Related Topics
How to Enable Standard Copy Paste for a Textview in Android
Error Loading the Sdk When Eclipse Starts
Android Keyboard Hides Edittext
Get Application Context Returns Null
Problem to Achieve Curved Animation
Wrong Requestcode in Onactivityresult
Broadcast Receiver Not Working After Device Reboot in Android
Jar Mismatch! Fix Your Dependencies
Android Studio:Unmappable Character for Encoding Utf-8
Session 'App': Error Launching Activity
Android Detect Done Key Press for Onscreen Keyboard
How to Remove All Listeners Added with Addtextchangedlistener
Theme/Style Is Not Applied When Inflater Used with Applicationcontext
How to Save Scroll Position of Recyclerview in Android
Disable Logcat Output Completely in Release Android App