0
votes

I have a Custom ListView with a webView inflated in it. here is XML code

reader_layout.xml

 <ListView
    android:id="@+id/listReader"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/readerHeader"
    android:divider="@android:color/background_dark"
    android:dividerHeight="5dp" >
</ListView>

reader_list_webview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >
<WebView
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:scrollbars="none" />
</RelativeLayout>
  • I am using the AsyncTask and BaseAdapter to fill the listView with WebView, and I have loaded different URL in each webView. here is the java Code.

        public class ImageAdapter extends BaseAdapter {
    
        public ImageAdapter(Context c) {
        context = c;
    }
    
    // ---returns the number of images---
    public int getCount() {
        return URLs.size();
    }
    
    public ImageAdapter(Context ctx, Activity act) {
        inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    // ---returns the ID of an item---
    public Object getItem(int position) {
        return position;
    }
    
    public long getItemId(int position) {
        return position;
    }
    
    // ---returns an ImageView view---
    public View getView(final int position, View convertView,
            ViewGroup parent) {
    
        // ImageView bmImage;
    
        final ViewHolder holder;
        View vi = convertView;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.book_reader_list_style, parent,
                    false);
            holder = new ViewHolder();
    
            holder.webView = (WebView) vi.findViewById(R.id.webView1);
    
            WebSettings webSettings = holder.webView.getSettings();
            webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
            holder.webView.setVerticalScrollBarEnabled(false);
            holder.webView.setHorizontalScrollBarEnabled(false);
            holder.webView.setVisibility(View.VISIBLE);
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setUseWideViewPort(false);
            holder.webView.setInitialScale(scaleWebView);
    
            vi.setTag(holder);
        } else {
    
            holder = (ViewHolder) vi.getTag();
        }
        holder.webView.loadUrl((URLs.get(position)));
    
        return vi;
    
    }
    

    }

    class ViewHolder { WebView webView; }

Everything is working fine. But problem is that some URLs becomes very wider then the webview size hence they doesn't fit the device screen width and some URLs load small pages hence we need to set zoom of webview.

  • I want to set the Zoom/scale of the webView according to the size of URLs page dynamically. so that user should not use zoom in or Zoom out.

I have tried this

android:layout_width="match_parent"
android:layout_height="match_parent"

and this

myWebview.setLoadWithOverviewMode(true);
myWebview.setUseWideViewPort(true);

and this also

holder.webView.setInitialScale(percent);

but it doesn't resolve my requirements.

Edit

if I m setting setLoadWithOverviewMode(true/false); to true my app crashes and here is the Logcat

01-08 16:16:56.600: E/AndroidRuntime(8666): java.lang.IllegalArgumentException: bitmap size exceeds 32bits
  • actually my webview loads images of 491X600 upto 300X500 in size. how can i resize the images or something like that? but when i set setLoadWithOverviewMode(true/false); to false it working fine but problem is that the webview doesn't re size to fit the width of screen.
2

2 Answers

1
votes

you can try this..

/**This is for to set wide */
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);
1
votes

Finally I sorted out my solution myself. Here it is

holder.webView.loadDataWithBaseURL(null,"<!DOCTYPE html><html><body style = \"text-align:center\"><img style=\"border-style:dotted;border-width:5px;border-color:black;width:99%;\" src= "  + URLs.get(position)+ " alt=\"page Not Found\"></body></html>","text/html", "UTF-8", null)

it will exactly fit the webView's tag fill the screen. no matter your phone is 230X480 or u have any android tablet of 800X1200 .