1

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.

8
  • 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? Commented Jan 14, 2020 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: stackoverflow.com/a/12802173/882251 Commented Jan 14, 2020 at 19:18
  • @Darwind correct- when I use Google, the browser loads within the app. Trying WebViewClient override now... Commented Jan 14, 2020 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? Commented Jan 14, 2020 at 19:26
  • You should probably just delete the question as I found the other question and answer by a quick search on Google :-) Commented Jan 14, 2020 at 19:53

1 Answer 1

4

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

webview.webViewClient = WebViewClient() 

For further details regarding WebView check here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.