Sindu Potha

Sindu Potha was created by us who wish to enjoy Sri Lankan songs.

How to Improve WebView Performance Android Studio?

How to Improve WebView Performance Android Studio?
How to Improve WebView Performance Android Studio? 100%

Table of Contents

Android WebView App performance optimization

In the latest Articles, we discussed,

How to Improve WebView Performance Android Studio?

Today we are going to optimize our Android WebView App. To do that open your Android Studio Project we already have created.

Improve WebView Performance Android Studio?

1. Now Copy and paste the following codes into your WebView App. (MainActivity.java)

//improve WebView Performance
myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);

2. Now Open Your AndroidMainfest.xml file and add the following code under MainActivity.

android:hardwareAccelerated="true"

Here are the full codes…

MainActivity.java

package com.techedutricks.techedutricks;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

private WebView myWebView;

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

myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings=myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

//improve WebView Performance
myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);

//Load Website
myWebView.loadUrl("https://www.google.com");
myWebView.setWebViewClient(new WebViewClient());
}

public void onBackPressed(){
if (myWebView.canGoBack())
{
myWebView.goBack();
}
else {
super.onBackPressed();
}
}
}

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.techedutricks.techedutricks">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TechEduTricks">

<activity android:name=".MainActivity"
android:hardwareAccelerated="true"
>

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

</manifest>

Run Your Application Now. Your WebView Android Application is Now optimized. In the next lessons, we improve the WebView app again and again. And also we will add more features to the WebView App.

Stay with us and share your experience with us…

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top