Android: loading a WebView image in the center

I want to upload an image to a web view, but it displays everything in the upper upper corner of the web view. My photo is not located on the Internet locally.

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 only my image is horizontal center how to center

+4
source share
5 answers

Try this, it will make the content suitable for your web browser.

 WebView web = (WebView) findViewById(R.id.gif_image); web.getSettings().setDefaultZoom(ZoomDensity.FAR); 
+1
source

Try it.

 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"); 
+10
source

I did this using 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 horizontal 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 is it for (horizontal and 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", ""); 
+3
source

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); 
+1
source

Do you use linear layout? Therefore, you will need to use the gravity property of the parent linear layout. Good explanation of gravity in android: http://sandipchitale.blogspot.com/2010/05/linearlayout-gravity-and-layoutgravity.html

0
source

Source: https://habr.com/ru/post/1432240/


All Articles