Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: WebView load image in center

I want to load an image at the web view, but it shows all at top_left of the web view. My picture is on the internet not local.

gifView.loadUrl(mImageUrl); 
gifView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            view.getSettings().setLoadsImagesAutomatically(true);
            if (mProgressBar != null) {
                mProgressBar.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (mProgressBar != null) {
                mProgressBar.setVisibility(View.VISIBLE);
            }
        }

    });

Here is my xml:::

  <WebView
                     android:id="@+id/gif_image"
                     android:background="@null"
                     android:layout_gravity="center"

                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />

Edit:i used:

   String HTML_FORMAT = "<html><body style=\"text-align: center; background-color: null; vertical-align: middle;\"><img src = \"%s\" /></body></html>";

         final String html = String.format(HTML_FORMAT, mImageUrl);

         gifView.loadDataWithBaseURL("", html, "text/html", "UTF-8", "");

but my image only Horizontal Center,how to center

like image 201
pengwang Avatar asked Sep 04 '12 10:09

pengwang


3 Answers

Try this.

myWebView.loadData("<html><head><style type='text/css'>body{margin:auto auto;text-align:center;} img{width:100%25;} </style></head><body><img src='www.mysite.com/myimg.jpeg'/></body></html>" ,"text/html",  "UTF-8");
like image 154
Chirag Avatar answered Nov 13 '22 17:11

Chirag


I did it by using some code and xml :

<WebView            android:id="@+id/webview"
                    android:layout_width="fill_parent"
                    android:layout_height="251dp"
                    android:layout_below="@+id/download"
                    android:layout_marginTop="20dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"/>

and this (for horizental center) :

WebView loading = (WebView)rootView.findViewById(R.id.webview);
                                loading.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
                                loading.loadDataWithBaseURL("file:///android_asset/", "<html><center><img src=\"done.png\"></html>", "text/html", "utf-8", "");

Or this for (horizontal & vertical center) :

WebView loading = (WebView)rootView.findViewById(R.id.webview);
                            loading.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
                            loading.loadDataWithBaseURL("file:///android_asset/", "<html>\n" +
                                    "<body bgcolor=\"white\">\n" +
                                    "    <table width=\"100%\" height=\"100%\">\n" +
                                    "        <tr>\n" +
                                    "            <td align=\"center\" valign=\"center\">\n" +
                                    "                <img src=\"loading.gif\">\n" +
                                    "            </td>\n" +
                                    "        </tr>\n" +
                                    "    </table>\n" +
                                    "</body>", "text/html", "utf-8", "");
like image 3
Khalil Kitar Avatar answered Nov 13 '22 18:11

Khalil Kitar


Try something like this:

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();

String data = "<body><center><img width=\"" + width + "\" src=\"" + url
              + "\" /></center></body></html>";
webview.loadData(data, "text/html", null);
like image 2
Shah Avatar answered Nov 13 '22 18:11

Shah