Abascus
Abascus

Reputation: 53

Android WebView isn't scrollable

My Android WebView isn't scrollable.

XML Code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#87cefa"
android:gravity="left"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<WebView
    android:id="@+id/webView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerHorizontal="true" />

I'm loading the Website with the normal webview.loadUrl(url); function and sometimes with the webview.loadDataWithBaseURL("", htmlContent, "text/html", "UTF-8", ""); function, both of them display the page, but they are not scrollable

Init:

setContentView(R.layout.activity_main); webview = (WebView)this.findViewById(R.id.webView); webview.setVerticalScrollBarEnabled(true); webview.setHorizontalScrollBarEnabled(true);

Loading:

            webview.loadUrl(url);
            [either one of them or the other]
    webview.loadDataWithBaseURL("", htmlContent, "text/html", "UTF-8", "");

Upvotes: 4

Views: 31939

Answers (8)

SWAPNIL PATIL
SWAPNIL PATIL

Reputation: 21

You must be using a scrollview, remove that scrollview and your webview will start scrolling.

Upvotes: 2

Olayiwola Osho
Olayiwola Osho

Reputation: 199

I was able to get my web view to scroll by adding

simpleWebView = findViewById(R.id.simpleWebView);

//setting the webView client to a new webview client 
simpleWebView.setWebViewClient(new WebViewClient());

//and Enabling JavaScript
simpleWebView.getSettings().setJavaScriptEnabled(true);

I would not advise Enabling JavaScript because Using setJavaScriptEnabled can introduce XSS vulnerabilities into your application and this just means if your web site is vulnerable malicious JavaScript can be returned to your users.

When the malicious code executes inside your users browser, the attacker can compromise their interaction with your application.

and I currently don't know any other way to enable JavaScript

Here's the full code I used below

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/simpleWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

And inside the MyActivity.java

simpleWebView.setWebViewClient(new WebViewClient());

simpleWebView.getSettings().setJavaScriptEnabled(true);

simpleWebView.setVerticalScrollBarEnabled(true);

simpleWebView.setHorizontalScrollBarEnabled(true);

Upvotes: 0

Yashaswi Bharadwaj
Yashaswi Bharadwaj

Reputation: 74

add this line and it will start working

webview.getSettings().setJavaScriptEnabled(true);

Hope, this solves your problem?

Upvotes: -1

Hey I had the Same Problem with webview. It was not scrolling even with the match_parent attribute and very simple code.

The problem is not in your xml file. Its in your Java file. In java file of that activity Override onPause AND onResume methods and iclude this code

@Override
protected void onResume() {
    super.onResume();
    browser.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    browser.onPause();
}

It is very important that you Pause your WebView AND then Resume it again. Hope that will solve your problem.

Upvotes: 0

Matthias Huber
Matthias Huber

Reputation: 21

I had some other elements in my relative layout, which I switched to invisible. This made my webview not scrollable. My solution was:

relativelayout.removeallchilds();
relativelayout.addchild(webview);
relativelayout.addchild(overlaybutton);

Upvotes: 1

Sreedhar GS
Sreedhar GS

Reputation: 2788

I have found a solution for this, All you need is to put WebView definition in "

layout/content_my.xml

" not in "activity_my.xml"

<WebView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/webView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

And inside the MyActivity.java

    WebView webview = (WebView)findViewById(R.id.webView);
    webview.loadUrl("file:///android_asset/index.html");
    webview.setVerticalScrollBarEnabled(true);
    webview.setHorizontalScrollBarEnabled(true);

Upvotes: 5

kabuko
kabuko

Reputation: 36302

Don't use wrap_content for your height. If your WebView has the same height as its content, then there's never anything to scroll as obviously the content all fits by definition (though given you're loading content dynamically this may not be exactly what's happening). Try setting the height to match_parent or a fixed value.

Upvotes: 3

Nathaniel D. Waggoner
Nathaniel D. Waggoner

Reputation: 2886

I've posted this as a comment as I think this may be a duplicate, but I would look at this question:

Android WebView Scrollable

Which provides a few calls which I believe will solve this issue.

Another issue:

Try changing your webview from "Wrap Content" to some specific height. Then set the scrollables.

Try something like 300dp and go from there.

Upvotes: 0

Related Questions