Skip to content Skip to sidebar Skip to footer

Android Center And Adjust Image In Webview

I've been working on programming a basic webcomic app lately and have been having issues with how webview is displaying the comics. The images themselves load fine, however there i

Solution 1:

Try setting the android:layout_height="wrap_content"

and about the whitespace issue, try looking into This Link


Solution 2:

Try this:

String dataString = "<head><style type='text/css'>"
                    +"body{margin:auto auto;text-align:center;} </style></head>"
                    +"<body><img src=\"image.png\"/></body>";

webview.loadDataWithBaseURL("file:///android_res/drawable/",dataString,"text/html","utf-8",null);

// set image scale to fit screen if larger than screen width
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
int screenWidth = displayMetrics.widthPixels; // minus some padding values if you have any left/right padding
Drawable drawable = getResources().getDrawable(R.id.image);
int wi = drawable.getIntrinsicWidth();

double scale = (wi>screenWidth) ? (double)screenWidth/wi : 1.0;
wv.setInitialScale((int) (scale*100));

In this case image.png is the file name and is located in res/drawable folder.


Post a Comment for "Android Center And Adjust Image In Webview"