Reputation: 305
Hello fellow programmers. Some background info, I'm quite new to android app development and decided to work on a personal project to practice my hand at it. In working on the project, most problems I encountered, I could overcome them through various Google searches. However this one issue has remained evident for quite a while now.
I want to display a YouTube channel in a webview. I've studied the other related forums but have found no working solution. If you want a working example of this, check out the "Markiplier Unofficial App", that makes use of a webview to display and play his YouTube videos. Below is the code I've so far for this activity.
The Webview layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/userOptionsLinearLayout"
android:background="#000000">
<WebView
android:id="@+id/activity_main_webview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|bottom" />
</LinearLayout>
The Custom WebViewClient:
import android.webkit.WebViewClient;
import android.webkit.WebView;
import android.net.Uri;
import android.content.Intent;
public class AJSYTWebViewClient extends WebViewClient{
String site_url_one = "www.youtube.com";
String site_url_two = "m.youtube.com";
// Used to judge which URL to keep open in app and which ones to divert to web browser or another app.
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().startsWith(site_url_one) || Uri.parse(url).getHost().startsWith(site_url_two)) {
return false; // Ensures URL opens in app
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
The WebView Activity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class ajs_youtube_page extends AppCompatActivity {
// Declaring Programming Webview variable.
private WebView mwebView;
// Primary URL.
String url = "www.youtube.com/user/AngryJoeShow";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ajs_youtube_page);
// Finding webview on layout screen.
mwebView = (WebView) findViewById(R.id.activity_main_webview);
//mwebView.setWebChromeClient(new WebChromeClient());
// Getting and adjusting WebView settings for javascript functionality.
WebSettings webSettings = mwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
// Force links and redirects to open in the WebView instead of in a browser
mwebView.setWebViewClient(new AJSYTWebViewClient());
mwebView.loadUrl(url);
}
// When the back button is pressed, it goes back to the previous screen.
@Override
public void onBackPressed() {
if(mwebView.canGoBack()) {
mwebView.goBack();
}
else {
super.onBackPressed();
}
}
}
Any guidance on this issue will be greatly appreciated.
Thanks.
Upvotes: 1
Views: 1512
Reputation: 305
Alright, so after reading through everyone's answers/suggestions and trying them out, unfortunately they did not work. Going on yet another research spree I finally found a solution. Please keep in mind that this isn't the best method performance wise, but for a new learner like me it works.
You need to add the following code into the activity calling your webview layout:
webSettings.setDomStorageEnabled(true);
I hope this helps someone. Below will be the link/URL to the question I found this answer at (surprised it evaded my eye all this time): Android webview won't load my URL but will load others
Thanks to all those who responded. Appreciate your input.
Upvotes: 1
Reputation: 25312
Update your code like below:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().startsWith(site_url_one) || Uri.parse(url).getHost().startsWith(site_url_two)) {
view.loadUrl(url);
return true; // Ensures URL opens in app
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
Upvotes: 0
Reputation: 1114
Change your URL from www.youtube.com to https://www.youtube.com/
Upvotes: 0