Reputation: 81
I have a simple program that should load youtube website in a webView when the app launches, but the app simply open native Youtube appliacation instead, but I need it to open Youtube website in the webView.
I tried looking in the "attributes" section in webView settings, but couldn't find anything that could enforce websites like this to be opened in the webView.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView web = findViewById(R.id.webV);
web.loadUrl("https://www.youtube.com/");
}
I want the "www.youtube.com" website to be opened in my WebView app, not in the native Youtube app that currently opens.
Upvotes: 1
Views: 78
Reputation: 4643
this due to youtube video url is opened in browser and browser redirects it to native app
add following code
web.setWebViewClient(new WebViewClient());
UPDATE
When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView. You can then allow the user to navigate backward and forward through their web page history that’s maintained by your WebView.
To open links clicked by the user, simply provide a WebViewClient for your WebView, using setWebViewClient().
Upvotes: 2