Billabong
Billabong

Reputation: 467

Webview image fit screen in Android

I load a .jpg in a WebView. My problem is that I found this: Android WebView, Scaling Image to fit the screen

and it doesn't work for me.

Here is my code:

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

    Toast.makeText(getApplicationContext(), ""+width, Toast.LENGTH_LONG).show();


    String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
    html+= "<body><img width=\""+width+"\"<img src=\""+"image.jpg"+"\" /></body></html>";

aboutText.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html","UTF-8" , null);

Upvotes: 0

Views: 6464

Answers (3)

BB_Dev
BB_Dev

Reputation: 511

You can style img tag before you load HTML into web view. Please see below code

WebView content = (WebView) findViewById(R.id.webView1);
String head = "<head> <style>img{display: inline;height: auto;max-width:   100%;}</style> <style>body {font-family: 'Roboto';  }</style></head>";

content.loadDataWithBaseURL(null, head + post.getContent(), "text/html", "UTF-8", null);

Upvotes: 4

Max
Max

Reputation: 1548

This wasn't working for me as i had high resolution phone.

Try this, it worked for me, webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

Upvotes: 5

Stack Overflow User
Stack Overflow User

Reputation: 4082

you are html image tag wrong check below code:

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

Toast.makeText(getApplicationContext(), ""+width, Toast.LENGTH_LONG).show();


String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
   html+= "<body><img width=\""+width+"\" src=\""+"image.jpg"+"\" /></body></html>";

aboutText.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html","UTF-8" , null);

your html string wrong formatted image tag like below code:

String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
html+= "<body><img width=\""+width+"\"<img src=\""+"image.jpg"+"\" /></body></html>";

formatted my code:

String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
html+= "<body><img width=\""+width+"\" src=\""+"image.jpg"+"\" /></body></html>";

Upvotes: 2

Related Questions