issue with WebView in android

  • Replies:0
vineesh
  • Forum posts: 1

Apr 26, 2014, 7:36:58 PM via Website

I am facing a strange issue with WebView in android .I wrote a sample application which displays some test message using webview .The Application has two activities, one activity simply contains a button, when user click on this button , it launch another activity which display the test message on a webview . I have set setInitialScale() of webview as 150, Now the issue is like, Normally webvbiew displays the content with the scale size 150 (as I set ) but some times it displays the contents with a scale greater than 150. I am wondering Why this is happening !!!!!!!

I did some investigation on this and my observations are given below

I overrided the onScaleChanged() function in webViewClient. In normal case before drawing the content on webview I am getting onScaleChanged() callback ,with the value [oldscale - 2 and new scale - 1.5]. When issue occurs , I am getting two onScaleChanged() callbacks before drawing the content!!! ,at first I get the onScalechange() callback with [oldscale as 2 and new scale as 1.5] , after that getting another onScaleChanged() callback with oldscale as 1.5 and new scale as 2.0 and hence it draws the webview with the scale 2.0 irrespective of setInitialScale() I have given in my application.

This issue happens only in the case if I set setInitialScale() to a value less than device's default webview scale. As per my understanding ,Every device has its own default Scale for webview based on their display property like .075 for ldpi and 1.5 for hdpi etc )

In my device defaultScale for webview is 2, and I set the setInitialScale() as 1.5(150) normaly webview reset the scale from 2 to 1.5 before draw ( That is from defult to new value ) and it works fine But some time after changing the scale to 1.5 it reverting back to 2. and issue occurs I am pasting my sample code here , Any body please let me know Why it is happening???? Is it becsuse of any implementation issue in my code ?? or is it the behaviour/bug/limitation of android ??? I have tested this in android 4.2 version

Thanks in advance ..

public class webActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    WebView webview= (WebView) findViewById(R.id.webView1);
    webview.getSettings().setLoadWithOverviewMode(false);
    webview.getSettings().setUseWideViewPort(false);
    webview.getSettings().setSupportZoom(false);
    webview.getSettings().setBuiltInZoomControls(false);
    webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    webview.setInitialScale(150);
    webview.setWebViewClient(new myWebClient());
    String html = "<html><body>test meassage test message test message test message test message</body></html>";
    String mime = "text/html";
    String encoding = "utf-8";
  //  myWebView.getSettings().setJavaScriptEnabled(true);
    webview.loadDataWithBaseURL(null, html, mime, encoding, null);          
}   
public class myWebClient extends WebViewClient
{

    @Override
    public void onScaleChanged(WebView view, float oldScale, float newScale) {
        // TODO Auto-generated method stub
        Log.e("WEBVIEW ","  >>>>>>>> ScaleChanged OldScale" +oldScale +"  New Scale :"+newScale);
        super.onScaleChanged(view, oldScale, newScale);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        return true;

    }
}

}

layout

<LinearLayout........................ android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >

&lt;WebView
    android:id=&quot;@+id/webView1&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot; /&gt;

</LinearLayout>

— modified on Apr 28, 2014, 12:48:14 PM by moderator

Reply