net ERR UNKNOWN URL SCHEME in Android Webview codes not working in Kotlin

+1 vote

I tested all of the Java-based codes from the forum. The IDE changed it to Kotlin, but it isn't operating. I recently began learning Android. Please, if you can, assist me. The converted code is attached below. I also included this:

  val webUrl = webView.url
    if (webUrl != null) {
        shouldOverrideUrlLoading(webView,webUrl)
    }

inside OnCreate. Not sure if that's correct or not. Below is the code---

private fun shouldOverrideUrlLoading(view: WebView, url: String) {
    view.loadUrl(url)
    if (url.startsWith("whatsapp://")) {
        view.stopLoading()
        try {
            val whatsappIntent = Intent(Intent.ACTION_SEND)
            whatsappIntent.type = "text/plain"
            whatsappIntent.setPackage("com.whatsapp")
            whatsappIntent.putExtra(
                Intent.EXTRA_TEXT,
                view.url.toString() + "  - Shared from webview "
            )
            startActivity(whatsappIntent)
        } catch (ex: ActivityNotFoundException) {
            val makeShortText = "Whatsapp has not been installed"
            Toast.makeText(this, makeShortText, Toast.LENGTH_SHORT).show()
        }
    }
}

I'm hoping for your prompt assistance. Thanks.

Nov 7, 2022 in Android by Edureka
• 12,690 points
8,892 views

1 answer to this question.

0 votes

3 ways to fix the err_unknown_url_scheme error.

You have two options for resolving this error: either disable certain schemes and only use the http:// and https:// schemes when developing your app, or modify your code to support these specific URL schemes.

Open In New Window

Editing your URL's href code is a quick and simple workaround. It is possible to circumvent the issue by appending target=" blank." Now that this link will now open in a new window, adding this line of code could have an unfavorable development effect.

It's not a good idea to open extra browser windows when developing. This is due to the fact that a new browser may annoy or confuse your user. Users may find it more challenging to navigate and return to their previous page in your app if the "back" button is removed. Target=" self" is the industry standard and opens the link in the same browser tab.

Add New Intent to Load in External App

Intents are used in Android development to tell Android what you want the app to perform, effectively letting you invoke a certain action or component. The addition of an intent to load in an external app can fix some scheme issues. For instance, the email link should open in your usual email application when you load mailto:/ in an external program. Another illustration is maps:/, which ought to launch your usual map program.

You will use an IF statement to manage this in the snippet of code below. Any URL with the prefix http:// or https:// is disregarded and opened normally. Otherwise, if the URL uses a unique scheme like:

  • tel://
  • sms://
  • mailto://
  • geo://
  • maps://

Disable any non-standard URL schemes

Disabling URL systems other than the typical https:// and http:// can entirely get around this issue. None of the previously mentioned custom schemes will be available to you.

A toast notification will appear whenever your app uses one of these navigational techniques or if a user accesses a link utilizing one of these techniques. The user won't be able to continue after seeing the warning "Error: Unknown link type."

Hope this works.....

answered Nov 8, 2022 by Edureka
• 13,620 points

Related Questions In Android

0 votes
0 answers

How to make grid view scroll horizontally not vertically in android?

I use a flexible Grid View. means ...READ MORE

Sep 21, 2022 in Android by Edureka
• 13,620 points
519 views
0 votes
0 answers

Google map not showing in android activity?

I want the MainActivity of my Android ...READ MORE

Nov 8, 2022 in Android by Edureka
• 13,620 points
2,096 views
0 votes
0 answers

How do I turn a C# object into a JSON string in .NET?

I have classes like these: class MyDate { ...READ MORE

Sep 20, 2022 in Android by Edureka
• 13,620 points
455 views
0 votes
0 answers

Factorial program in android

Hey guys, I am aware that there ...READ MORE

Sep 21, 2022 in Android by Edureka
• 13,620 points
4,814 views
–1 vote
1 answer

"Default Activity Not Found" on Android Studio upgrade

If you see that ERROR occurrence after ...READ MORE

answered Feb 11, 2022 in Others by Soham
• 9,710 points
1,232 views
0 votes
1 answer

Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio

In Android Studio 3.1, you can see ...READ MORE

answered Feb 17, 2022 in Java by Aditya
• 7,680 points
13,244 views
0 votes
1 answer
0 votes
0 answers

Android Studio Could not initialize class org.codehaus.groovy.runtime.InvokerHelper

I'm a newbie to Android Studio. Following ...READ MORE

Sep 22, 2022 in Android by Edureka
• 13,620 points
1,192 views
0 votes
1 answer
0 votes
1 answer

How get permission for camera in android.(Specifically Marshmallow)?

To begin with, confirm that the user ...READ MORE

answered Nov 8, 2022 in Android by Edureka
• 13,620 points
3,737 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP