All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

WebView Not Loading URL Android

Last Updated : Mar 11, 2024

WebView Not Loading URL Android

In this article we will show you the solution of WebView not loading URL android, WebView can be used to serve a web application (or merely a web page) as a component of a client application.

A web page can be displayed as part of your activity layout using the WebView class, which is an extension to Android's View class.

It lacks elements seen in a fully functional web browser, like navigation controls and an address bar.

By default, WebView just displays web pages. When you want to include content that you might need to update in your app, for instance an end-user agreement or even a user guide, using WebView is frequently advantageous.

To display your content that is hosted online, you can build an Activity inside of your Android app that includes a WebView.

If your programme gives the user access to data that must always be retrieved from the Internet, like email, WebView can be useful in this situation.

In this situation, creating a WebView inside your Android app that displays the web page filled with user data may be simpler than making a network request, analyzing the response, and then rendering the data in an Android layout.

Step By Step Guide On WebView Not Loading URL Android :-

<?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:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="4dp"
   tools:context=".MainActivity">
   <TextView
      android:id="@+id/textView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="5dp"
      android:textColor="#000000"
      android:textSize="24sp" />
   <WebView
      android:id="@+id/webView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@id/textView" />
   <Button
      android:id="@+id/btnLoad"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:text="Load URL" />
</RelativeLayout>

Java

package app.com.kotlinapp
import android.os.Bundle
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
   private var webView: WebView? = null
      private var textView: TextView? = null
      private var btnLoad: Button? = null
      override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
         textView = findViewById(R.id.textView);
         btnLoad = findViewById(R.id.btnLoad);
         webView = findViewById(R.id.webView);
         webView!!.webViewClient = WebViewClient()
         btnLoad!!.setOnClickListener(View.OnClickListener {
            webView!!.webViewClient = object : WebViewClient() {
               override fun onPageFinished(view: WebView, weburl: String) {
                  Toast.makeText(this@MainActivity, "Your WebView is Loaded....",
                  Toast.LENGTH_LONG).show()
               }
            }
            webView!!.webChromeClient = object : WebChromeClient() {
               override fun onProgressChanged(view: WebView, newProgress: Int) {
                  textView!!.text = "Page loading : $newProgress%"
                  if (newProgress == 100) {
                     textView!!.text = "Page Loaded."
                  }
               }
            }
            webView!!.loadUrl("http://www.tutorialspoint.com")
      })
   }
}

Android manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="app.com.myapplication">
<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/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>
  1. First we have to create an import function with the name android.app.activity.
  2. Then we create a class with an activity.
  3. Then we create a private webview function.
  4. Then we create an override function.
  5. Then we create a public void as a bundle saved instance state.
  6. Then we create webview.load URL for the creation of link.

Conclusion :-

In the WebView, JavaScript is just by default turned off.With the WebSettings connected to your WebView, you can make it active.

Get WebSettings using the getSettings() method, and then turn on JavaScript using the setJavaScriptEnabled method().

I hope this article on WebView not loading URL android helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪