1

Here's my code for that:

public void mywebview(){
try {
    webview.setBackgroundColor(0);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.addJavascriptInterface(new MyJavaScriptInterface(), "MYOBJECT");
    webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webview.getSettings().setUseWideViewPort(true);
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setBuiltInZoomControls(true);
    webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.getSettings().setAllowFileAccess(true);
    webview.canScrollHorizontally(1);
    webview.setInitialScale(1);
    webview.getSettings().setDefaultFontSize(2);

    webview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            webview.setHorizontalScrollBarEnabled(true);
            webview.setInitialScale(1);
            return false;
        }
    });

    webview.setWebViewClient(new WebViewClient() {


        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(view.getContext(), "Authentication Error", Toast.LENGTH_LONG).show();
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setCookie(USERNAME, PASSWORD);

            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

                return super.shouldOverrideUrlLoading(view, url);
            }
        }

        public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            SharedPreferencesManager mgr = SharedPreferencesManager.getInstance();
            StringBuilder stringBuilder = new StringBuilder();
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(URL);
            try {
                HttpResponse response = client.execute(httpGet);
                StatusLine statusLine = response.getStatusLine();

                int statusCode = statusLine.getStatusCode();
                if (statusCode == 200) {

                }

            } catch (Exception e) {

            }
            CookieStore cookieStore = mgr.getCookieStore();
            Cookie cookie = cookieStore.getCookies().get(1);

            CookieManager cookieMgr = CookieManager.getInstance();
            cookieMgr.setCookie(url, cookie.getName() + "=" + cookie.getValue());
            CookieSyncManager.getInstance().sync();

            return super.shouldInterceptRequest(view, url);
        }
    });

    webCookieManager = CookieManager.getInstance();

    webCookieManager.setAcceptCookie(true);
    CookieStore cookieStore = mgr.getCookieStore();
    Cookie cookie = cookieStore.getCookies().get(1);

    webCookieManager.setCookie(URL, cookie.getName() + "=" + cookie.getValue() + ";");
    CookieSyncManager.getInstance().sync();
    webview.loadUrl(URL);
} catch (Exception e) {
    e.printStackTrace();
}
}

It seems like webresourceresponse is compatible with only with lollipop version and above. Is there a way I can fix it to work for lower versions namely jelly bean and kitkat? Also, by "messes" up I mean it looks all vertically aligned stuffed together into one view, unlike for lollipop+ version where the view looks decently organized as expected. Here's my xml for the same:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myholder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
     >
 <WebView
        android:id="@+id/my_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>
Sreehari
  • 5,621
  • 2
  • 25
  • 59

2 Answers2

0

i don't think there is a way you can change that unless you use the old way for old API see this

Community
  • 1
  • 1
njihia
  • 9
  • 2
-1

The best/only way to do this would be to write version specific code, one for jelly bean and kitkat, and one for lollypop and above. I haven't looked into code that would make it work on the lower versions but the way to approach it would be to check for the SDK version, and execute the code for the specific version.

Example: (code modified from: How to support multiple android version in your code?)

// System information
private final int sdkVersion = Build.VERSION.SDK_INT;
// If you want to go really old:
// (actually, there is a question about how this issue should be handled
// systematically. Suggestions welcome.)
// final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);

// For meaning of other variable, see notification documentation on the android website.
if (sdkVersion > Build.VERSION_CODES.LOLLIPOP) {
    myWebviewForLollipopAndAbove webview = new myWebviewForLollipopAndAbove();
    //Do things with the webview, finish implementing
}
else {
    myWebviewForBelowLollipop webview = new myWebviewForBelowLollipop();
    //Do things with the webview, finish implementing
}

Obviously this code would have to be placed where the web view is initialized - and you will need to make another webview class for the other SDK versions.

Community
  • 1
  • 1
jrobe
  • 107
  • 5
  • but thats not the issue I am facing. The issue is if public WebResourceResponse shouldInterceptRequest(WebView view, String url) can be made backward compatible somehow? –  Apr 05 '16 at 18:10