How can i load image url to fit my webview in android?

Raghu Mudem picture Raghu Mudem · Nov 22, 2012 · Viewed 10.3k times · Source

I am developing an android application.Here i am displaying a url image in webview. But the image is not fit to my webview. some one is suggested to use viewport concept. But i am not able to use that perfectly. I attached my issue in drawing form with explanation. I used

mWebView.getSettings().setUseWideViewPort (true);

but not get the good result. I need to show the fitted image in all resolution devices including 7inch, 10 inch tablets. So please look at the attached image and suggest me the right way to fit url image in my webview place. I would like to show my image same as in 3rd diagram.

Here is my code for loading image:

webView = (WebView) findViewById(R.id.webimage);
        WebSettings webSettings = imageView.getSettings(); 
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setSupportZoom(true) ;
        webSettings.setBuiltInZoomControls(true);
        webView.setBackgroundColor(Color.TRANSPARENT);
        webView.getSettings().setUseWideViewPort (true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.setWebViewClient(new WebViewClient()
        {
        //some dialog implementation
        }
        webView.loadUrl(myurl);

enter image description here

Answer

Bhavesh Patadiya picture Bhavesh Patadiya · Nov 22, 2012

you can set width attribute of img to 100% and don't set height attribute

<body >
<img id="resizeImage" src="you_image.png" width="100%" alt="" />
</body>

Edited : Actually i also Having the Same Problem. However Still i am Waiting for the Correct Answer.

you can refere my Question HERE

though i have managed some how this Problem with a Trick by managing height and Width of Device. and Using wv.setInitialScale(185); ByDefault the Zoom will be in Top Left Corner.

i have put my code below.

WindowManager mWinMgr;
    public static int displayWidth = 0, displayHeight = 0;

    @Override
    protected void onCreate(Bundle icicle) {
        // TODO Auto-generated method stub
        super.onCreate(icicle);
        // this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        // this.setProgressBarVisibility(true);
        mWinMgr = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        displayWidth = mWinMgr.getDefaultDisplay().getWidth();
        displayHeight = mWinMgr.getDefaultDisplay().getHeight();

        setContentView(R.layout.view_my_poops);

        try {
            Map_Type = getIntent().getExtras().getString("MAP_TYPE");
        } catch (Exception e) {

        }
        Log.i(TAG, "MAP TYPE" + Map_Type);

    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        WebView wv;

        wv = (WebView) findViewById(R.id.webview_MyPoops);
        wv.getSettings().setJavaScriptEnabled(true);
        // this.setProgressBarVisibility(true);
        if (displayWidth >= 552 && displayHeight >= 976 || displayWidth >= 976
                && displayHeight >= 552) {
            wv.setInitialScale(185);
        } else {
            // wv.setInitialScale(150);
        }
        // wv.setInitialScale(30);


        URL = "YOUR_URL";


        Log.i(TAG, "Loading URL" + URL);

        final Activity activity = ViewMyPoop.this;

        wv.setWebChromeClient(new WebChromeClient() {

            public void onProgressChanged(WebView view, int progress) {
                // TODO Auto-generated method stub
                // Log.i(TAG, "Inside OnProgress Changed" + URL);
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
                if (progress == 100)
                    activity.setTitle("My title");
            }
        });

        wv.loadUrl(URL);
        wv.setWebViewClient(new WebViewClient());
}

Hope it will Some How Help you.