0

I have inserted the below code into MainActivity.kt in a blank activity.


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val url = "http://www.yahoo.com/"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Get the web view settings instance
        val setting = webview.settings;

        // Enable java script in web view
        setting.javaScriptEnabled = true

        webview.loadUrl(url)

    }
}

I also have inserted a WebView component into activity_main.xml with an id of @+id/webview. When the app loads, the screen redirects to a chrome browser where the Yahoo page is loaded. How do I maintain the browser within the app?

I have tried adding webview.setWebViewRenderProcessClient = webViewClient() before the URL loads, but I get an unresolved reference error when I compile the code.

EDIT

The XML is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

I have also added <uses-permission android:name="android.permission.INTERNET"/> to the AndroidManifest.xml file.

Mr T
  • 950
  • 8
  • 26
  • 1
    There's nothing in your code that would make the default browser open when you load the URL. Do you have any attributes set in your XML layout perhaps? – Darwind Jan 14 '20 at 19:12
  • 1
    Ok, I just tried the code and it seems like it has something to do with your URL. Try with `google.com` for instance and it shouldn't open the default browser, but just load the website. Try overriding the `WebViewClient` also as stated in this SO thread: https://stackoverflow.com/a/12802173/882251 – Darwind Jan 14 '20 at 19:18
  • @Darwind correct- when I use Google, the browser loads within the app. Trying WebViewClient override now... – Mr T Jan 14 '20 at 19:23
  • Changing ```webview.webViewRenderProcessClient = webViewClient()``` to ```webview.webViewClient = WebViewClient()``` has done the job! Thanks. Add it as an answer and I'll mark it as correct? – Mr T Jan 14 '20 at 19:26
  • You should probably just delete the question as I found the other question and answer by a quick search on Google :-) – Darwind Jan 14 '20 at 19:53
  • Does this answer your question? [Android webview launches browser when calling loadurl](https://stackoverflow.com/questions/7746409/android-webview-launches-browser-when-calling-loadurl) – Darwind Jan 14 '20 at 20:35
  • @Darwind I'll leave this on here as it relates to Kotlin rather than Java – Mr T Jan 14 '20 at 22:31
  • It's not really Kotlin related per se, but sure. :-) – Darwind Jan 15 '20 at 10:50

1 Answers1

3

Implement WebView's webClient and set it before calling webView.loadUrl = "#someURL". Example:

webview.webViewClient = WebViewClient()

For further details regarding WebView check here

Gaurav Singh
  • 1,650
  • 12
  • 29