Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load image from url in WebView ( center vertically )

I'll try load image in a webview, but the image is position in top, and i need that their orientation is center vertically.

enter image description here

This is the code:

. . .

mStringUrl = getIntent().getExtras().getString(ARGS_IMAGE_URL);

. . .

private void initializeWebView() {

    mWebView.loadDataWithBaseURL(null, "<html><head><style>img {margin-top:auto;margin-bottom:auto}</style></head><body><img src=\"" + mStringUrl + "\"></body></html>", "html/css", "utf-8", null);

    WebSettings settings = mWebView.getSettings();
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);

    mWebView.setBackgroundColor(getResources().getColor(R.color.transparent));

    mWebView.setWebViewClient(new CustomWebViewClient());

}

xml file:

<include layout="@layout/layout_toolbar" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:background="@color/black_midnight" />

    <ProgressBar            
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:visibility="gone"
        style="@android:style/Widget.Material.ProgressBar.Small"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

If any have idea please help me, Thanks in advance.

like image 687
dpulgarin Avatar asked Sep 26 '22 13:09

dpulgarin


1 Answers

Try this code:

mWebView.loadDataWithBaseURL(null, "<html><head></head><body><table style=\"width:100%; height:100%;\"><tr><td style=\"vertical-align:middle;\"><img src=\"" + mStringUrl + "\"></td></tr></table></body></html>", "html/css", "utf-8", null);
like image 115
joan16v Avatar answered Sep 28 '22 04:09

joan16v