Android Webview Slow

Android webview slow

It depends on the web application being loaded. Try some of the approaches below:

Set higher render priority (deprecated from API 18+):

webview.getSettings().setRenderPriority(RenderPriority.HIGH);

Enable/disable hardware acceleration:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// chromium, enable hardware acceleration
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
// older android version, disable hardware acceleration
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

Disable the cache (if you have problems with your content):

webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

Android webview loading data performance very slow

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);
}

Android 19 has Chromium engine for WebView. I guess it works better with hardware acceleration.

For more info Android 4.4 KitKat, the browser and the Chrome WebView

Hardware Acceleration also do's the trick.You can use it in different levels in your application.

Application level

<application android:hardwareAccelerated="true" ...>

Activity level

<application android:hardwareAccelerated="true">
<activity ... />
<activity android:hardwareAccelerated="false" />
</application>

Window level

getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

View level

myView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

But as already mentioned in your question can you elaborate the side effects for this ?

Android Webview is slow


1.build.gradle(Project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
maven {
url 'https://download.01.org/crosswalk/releases/crosswalk/android/maven2'}
} // this is the single line you are adding to project level gradle file.else is generated.
}

task clean(type: Delete) {
delete rootProject.buildDir
}

  1. build.gradle(app)

    apply plugin: 'com.android.application'

    android {
    compileSdkVersion 25 //put your desired version
    buildToolsVersion "25.0.0" // put your desired version
    defaultConfig {
    applicationId "com.example.arsensench.xwalk" //put your package name
    minSdkVersion 16 // min sdk should be 16
    targetSdkVersion 25 // target sdk can be your desierd
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    }

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'
    //until here everything is generated
    //the line below is using the latest XWALK.
    compile 'org.xwalk:xwalk_core_library:22.52.561.4'

    }

3. AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arsensench.xwalk"><!-- your package name-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

4.MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.arsensench.xwalk.MainActivity"> <!--your class path-->

<org.xwalk.core.XWalkView
android:id="@+id/xwalkWebView"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
/>
</RelativeLayout>

5.MainActivity.java()

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import org.xwalk.core.XWalkView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

XWalkView xWalkWebView=(XWalkView)findViewById(R.id.xwalkWebView);
xWalkWebView.load("http://208.77.22.13/proprek_android/property-listing-trends.php?PropertyID=63&placeId=1&property_type=1&BuildingName=Arabian%20Ranches%20&BedRooms=4", null);
}
}

android webview loadurl slow

Unofortunately, there won't be much you can do to fix this. However:

  • Try to load your URL in the browser on your android device. Is it faster ? If not, there's not really much you can do.

There's a couple of things you can try though, and a few things to check. Specifically:

  • You're setting the visibility to View.GONE (making your webview invisible) while the page is loading, and then making it visible again when the page has loaded. This is probably the problem.

    Try without this, and you will probably find that it will be quicker. In my experience, onPageFinished(..) only fires some time after the page is loaded.

  • Does the page really require JavaScript ? If not, don't enable it.

  • If it's feaseable in your case, you can use a HTML parser like Jsoup to extract only the desired data from the page, and show that to the user. This will be a lot faster.

    If the page uses Ajax to load data dynamically, you can also load the data directly from the endpoints it uses. Open the page in a desktop browser, and open the network tab of developer tools to find out how the page works and loads data.

  • You can block requests from the WebView with shouldInterceptRequest(..). This may help if the page has things like eg. Facebook share buttons or extra images which you don't need. Blocking these will speed up load times.

If you show us the URL you're using, maybe I can investigate more and tell you axactly how you could speed it up in your case. Let me know if it helps.



Related Topics



Leave a reply



Submit