sadab
sadab

Reputation: 59

webview not scrolling automatically in android

In my webview app one of the webpage contains elements at the center position only.In order to make the webpage compatible to mobile display I need the center part to fill the display by zooming.I used setInitialScale() and scrollTo() functions .But I am not getting what i need. please help me.The zooming is working .But it is not scrolling to the center part.It remains in the top-left that is blank space only.

AdminActivity.java

package com.example.admin;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressWarnings("deprecation")
public class AdminActivity extends Activity {

private WebView webView;

@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        webView = (WebView)findViewById(R.id.webView4);
        webView.requestFocusFromTouch();

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);                                                         
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);

        webView.loadUrl("http://app.samworkshops.org/PasswordProtect.aspx");

        webView.setWebViewClient(new WebViewClient() {


            @Override
            public void onPageFinished(WebView view, String url) {


                webView.scrollTo(100,100);
                webView.setInitialScale(100);
            }


        });
    }


}

activity_admin.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView4"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:windowSoftInputMode="adjustResize"
/>

Upvotes: 1

Views: 7657

Answers (2)

Anuj Sharma
Anuj Sharma

Reputation: 488

WebView v = (WebView) findViewById(R.id.webview); v.setVerticalScrollBarEnabled(true); v.setHorizontalScrollBarEnabled(true);

That's it!

Upvotes: 0

Dakshesh Khatri
Dakshesh Khatri

Reputation: 629

package com.example.admin;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressWarnings("deprecation")
public class AdminActivity extends Activity {

private WebView webView;

@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        webView = (WebView)findViewById(R.id.webView4);
   // for scrollabale webview
        webView.setVerticalScrollBarEnabled(true);
        webView.setHorizontalScrollBarEnabled(true);
        webView.requestFocusFromTouch();

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);                                                         
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);

        webView.loadUrl("http://app.samworkshops.org/PasswordProtect.aspx");

        webView.setWebViewClient(new WebViewClient() {


            @Override
            public void onPageFinished(WebView view, String url) {


                webView.scrollTo(100,100);
                webView.setInitialScale(100);
            }


        });
    }

Upvotes: 0

Related Questions