3

I am facing problem regarding load a url in android below 5.0(lollipop) It is too slow to load data but in android 5.0 or above its working fine. how to resolve this issue? I have follow this link . & How to improve webview load time

my code is

   @SuppressLint({ "SetJavaScriptEnabled", "DefaultLocale" })
      public class YS_WebViewActivity extends Activity
     {
    private Button btnDone;
    private WebView webViewLoadUrl;
    private YS_GeneralUtility generalUtility;
    private Dialog alertDialogBox;
    private String url = "";
    private TextView tvLoading;
    private Button btnCancel;
    private Button btnRetry;

    @SuppressLint("InlinedApi")
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        btnDone = (Button) findViewById(R.id.btnDone);
        webViewLoadUrl = (WebView) findViewById(R.id.webViewLoadUrl);
        //webViewLoadUrl.setBackgroundColor(Color.parseColor("#123456"));
        tvLoading = (TextView) findViewById(R.id.tvLoading);
        url = getIntent().getExtras().getString("url");
        webViewLoadUrl.setVisibility(View.INVISIBLE);
        generalUtility = new YS_GeneralUtility(this);

        webViewLoadUrl.getSettings().setJavaScriptEnabled(true); // enable javascript
        try {
            webViewLoadUrl.getSettings().setRenderPriority(RenderPriority.HIGH); 
            webViewLoadUrl.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            webViewLoadUrl.getSettings().setDomStorageEnabled(true);
            webViewLoadUrl.getSettings().setAllowFileAccess(true);
            webViewLoadUrl.getSettings().setAllowContentAccess(true);
            webViewLoadUrl.getSettings().setAllowFileAccessFromFileURLs(true);
            //webViewLoadUrl.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
        } catch (Exception e) {
            e.printStackTrace();
        }
        tvLoading.setTypeface(generalUtility.faceAvenirLight);
        btnDone.setTypeface(generalUtility.faceAvenirMedium);
        btnDone.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                myErrorFinish();
            }
        });
        webViewLoadUrl.setWebViewClient(new WebViewClient()
        {
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                //Log.i(TAG, "Processing webview url click...");
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url)
            {
                //Log.i(TAG, "Finished loading URL: " + url);
                webViewLoadUrl.setVisibility(View.VISIBLE);
                tvLoading.setVisibility(View.INVISIBLE);
            }

            String errorData = "<html><head></head>"
                    + "<body style=' font-family:Avenir !important;font-weight:lighter !important; color:#ffffff; margin : 0px auto;'>"
                    + "<div style = 'width : 100% ; height : 100% ; background:#123456;'> </div>"
                    + "</body></html>";

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
            //  Log.e(TAG, failingUrl + "Error: " + description);
                view.loadData(errorData, "text/html", "UTF-8");
                alertDialogOnInternetOff(getString(R.string.errorMsgInternet));
                // finish();
            }
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }
        });

        webViewLoadUrl.loadUrl(url);
        // setContentView(webview);
    }


    private void alertDialogOnInternetOff(String message)
    {
        alertDialogBox = new Dialog(YS_WebViewActivity.this, R.style.InternetErrorPopup);
        alertDialogBox.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alertDialogBox.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        alertDialogBox.setContentView(R.layout.internetpopup_dialog);
        alertDialogBox.setCancelable(false);
        alertDialogBox.show();
        btnCancel = (Button) alertDialogBox.findViewById(R.id.btnCancel);
        btnRetry = (Button) alertDialogBox.findViewById(R.id.btnRetry);
        TextView txtExitMessage = (TextView) alertDialogBox.findViewById(R.id.textViewMessage);
        TextView horizontalLinePopup = (TextView) alertDialogBox.findViewById(R.id.horizontalLinePopup);
        TextView verticalLinePopup = (TextView) alertDialogBox.findViewById(R.id.verticalLinePopup);
        horizontalLinePopup.setBackgroundColor(getResources().getColor(R.color.internetpopupBorderinnerpageColor));
        verticalLinePopup.setBackgroundColor(getResources().getColor(R.color.internetpopupBorderinnerpageColor));
        txtExitMessage.setTypeface(generalUtility.faceAvenirLight);
        btnCancel.setTypeface(generalUtility.faceAvenirLight);
        btnRetry.setTypeface(generalUtility.faceAvenirLight);
        txtExitMessage.setText(R.string.errorMsgInternet);
        btnCancel.setText(R.string.cancel);
        btnRetry.setText(R.string.retry);
        alertDialogBox.setCancelable(false);
        btnCancel.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                alertDialogBox.dismiss();
                Log.d("AlertDialog", "Negative");
                YS_WebViewActivity.this.finish();
            }
        });
        btnRetry.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                alertDialogBox.dismiss();
                Log.d("AlertDialog", "Positive");
                tvLoading.setVisibility(View.VISIBLE);
                webViewLoadUrl.loadUrl(url);

            }
        });
    }
    private void myErrorFinish() 
    {
        /*
         * Jump to the Setting Screen on pressing back button of device
         */
        YS_WebViewActivity.this.finish();
        //overridePendingTransition(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom);
    }

    @Override
    public void onBackPressed() 
    {
        myErrorFinish();
        super.onBackPressed();
    }
  }

This is XML...

     <RelativeLayout 
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/listback"
        android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="42dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/navigationbackground" />

    <Button
        android:id="@+id/btnDone"
        android:layout_width="wrap_content"
        android:layout_height="42dip"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/transparent"
        android:text="@string/done"
        android:textColor="@android:color/white"
        android:textSize="17sp" />

    <WebView
        android:id="@+id/webViewLoadUrl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/btnDone"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/tvLoading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/loading"
        android:textColor="@android:color/white"
        android:textSize="18sp" />


    </RelativeLayout>
Community
  • 1
  • 1

3 Answers3

2

With Android 4.4 (October 2013), Google switched its WebView to use the Chromium rendering engine — a move that brought substantial speed improvements, numerous new APIs, and the ability to remote debug with the Chrome DevTools.

According to this Google’s documentation they have updated some more additional features in the Android 5.0 which brings more high performance to the webview.

The initial release for Android 5.0 includes a version of Chromium for WebView based on the Chromium M37 release, adding support for WebRTC, WebAudio, and WebGL.

Chromium M37 also includes native support for all of the Web Components specifications: Custom Elements, Shadow DOM, HTML Imports, and Templates. This means you can use Polymer and its material design elements in a WebView without needing polyfills.

Although WebView has been based on Chromium since Android 4.4, the Chromium layer is now updatable from Google Play.

So, according to the high end changes in the webview it vll load the data efficiently compare with the lower versions.

For your question, you can speed up the android webview performance in your case, like Android webview slow and Android WebView performance or like Android webview loading data performance very slow

Hardware Acceleration vll do's the trick.You can use it in different levels in your application in this answer i mentioned the other types as well.

I think the following works best:

if (Build.VERSION.SDK_INT >= 19) {
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}       
else {
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

Hope this answer will helps you.

Community
  • 1
  • 1
King of Masses
  • 18,405
  • 4
  • 60
  • 77
1

If after all other answer your problem is still remains then you need to test your URL/server response and data format.

MPG
  • 785
  • 6
  • 20
  • thanks MPG.. it was stupid mistake in response that's why not showing data. –  Sep 09 '15 at 12:37
0

Try

webview.getSettings().setRenderPriority(RenderPriority.HIGH);
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39