Android: Show youtube channel in webview using nexus 4 - android

I'm using a webview to show a youtube channel.
Using a nexus 4 it is not possible to play video (only audio starts).
I need to maintain the same graphic of the m.youtube.com/user/someuser so i can't use youtube api
Here is my code
public class MainActivity extends Activity {
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
final Activity activity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 1000);
}
});
mWebView.setWebViewClient(new MyOwnWebViewClient());
mWebView.loadUrl("http://m.youtube.com/user/someuser");//a channel youtube url
mWebView.setWebViewClient(new MyOwnWebViewClient());
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public class MyOwnWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
I have googled a lot but I can't find a solution.
The last code i took is from this post:
Youtube Video in WebView doesn't load
Could anyone help me?
Thanks thousand

Related

Don't navigate to other pages in WebView,disable links and references and go back to first page

I have a webView in Android and I open a html webpage in it. But it's full of links and images: when I click one of them it loads in my webview.I want to disable this behaviour, so if I click on a link, don't load it and go back to the first page.
I've tried this solution and edited a bit for myself, but it does not work:
webSettings.setJavaScriptEnabled(myWebViewClient.equals(true));
This opens a white page but I want to open the main URL. My webviewclient code:
public class MainActivity extends Activity {
public static String URL = "http://www.example.com/";
private WebView webView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.webView = (WebView) findViewById(R.id.webview);
MyWebViewClient myWebViewClient = new MyWebViewClient();
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(myWebViewClient.equals(true));
webView.reload();
webView.loadUrl(URL);
webSettings.setDisplayZoomControls(true);
webView.setWebViewClient(new WebViewClient());
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack()){
webView.goBack();
return true;
}
break;
}
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
if (webView.canGoBack()){
webView.goBack();
}
else{
super.onBackPressed();
}
}
}
you need to overwrite onPageStarted in WebViewClient:
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//here check url i.e equals to your that html page
// if url equals not load url
and for go to back page check:
if(view.canGoBack())
// load first page url
}
You need a WebViewClient that overrides loading url:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// url is the url to be loaded
return true; // override url (don't load it)
return false; // let url load
}
});
You can always return true to stay on the same page.
To open Main Url and stay on it, use this:
webview.loadUrl(MainUrl);
and then override loading url and always return true:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
});

WebView not showing standard browser option menu

Good morning,
I want that when the user opens my custom WebView, when he clicks the Menu Button, the standard browser's menu will appear (next, previous, share link, bookmarks, etc.).
How?
Here's my code:
public class WebViewActivity extends Activity {
private static final String TAG = "WebViewActivity";
WebView mWebView;
ProgressBar loadingProgressBar, loadingTitle;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Shared.openedActivities.add(this);
String url = this.getIntent().getExtras().getString("url");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
mWebView.setWebViewClient(new MyWebViewClient());
WebSettings webSettings = mWebView.getSettings();
webSettings.setPluginState(PluginState.ON);
loadingProgressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
mWebView.setWebChromeClient(new WebChromeClient() {
// this will be called on page loading progress
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
loadingProgressBar.setProgress(newProgress);
// hide the progress bar if the loading is complete
if (newProgress == 100) {
loadingProgressBar.setVisibility(View.GONE);
} else {
loadingProgressBar.setVisibility(View.VISIBLE);
}
}
});
}
public void onDestroy() {
super.onDestroy();
Trace.w(TAG, "onDestroy()");
Shared.unbindDrawables(findViewById(R.id.rootWebViewActivity));
System.gc();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return super.onKeyDown(keyCode, event);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Sorry but that won't work, you have to implement this features by yourself. WebView is not the Browser, it is just a View to render Webpages. Everything else is a function of the Browser-App not the WebView.
Nevertheless you can access the bookmarks of the browser as described here: Get browser history and search result in android

WebView also Gone when Sreen Time-Out

I would like to find a solution to allow my WebView still alive when screen time-out.
I am program to play music using WebView, it's working very properly if the screen alive but when the screen time-out my music in WebView also GONE.
Here is my code:
public class WebViewActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl("http://mp3skull.com/usher.html");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onPause()
{
mWebView.setVisibility(View.GONE);
mWebView.destroy();
super.onPause();
}
}
I am very shy to tell you that i tried to find this solution over a week but still can't done :(.
Hope you give me solution on this matter.
Thanks you.
Remove onPause() method in your class like below code:
public class WebViewActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl("http://mp3skull.com/usher.html");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

Stop the Music in WebView when Screen Time-Out

I am programming that allow users can listen music in WebView via the URL web link.
But I wonder why when the android phone user screen Time-Out my music in WebView also stop.
Do you have any solution to solve this problem?
Here is my code:
public class PlayMusicActivity extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl("http://www.myweb.com/music.html");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onPause() {
mWebView.setVisibility(View.GONE);
mWebView.destroy();
super.onPause();
}
}
Appreciate for your help.
Best regards,
Virak

Android : Simple web application crashes on links other then the http scheme

I am a beginner in developing for the Android, so excuse me if this is obvious. I created a simple web application using the samples from android.com however, when a link like tel: or mailto: is clicked, the application crashes and askes me to force quit. I was wondering if anyone could tell me why.
public class MyApplication extends Activity {
WebView mWebView;
private class InternalWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This is my web site, so do not override; let my WebView load the page
if (Uri.parse(url).getHost().equals("m.mydomain.nl")) {
return false;
} else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the WebView class to display website */
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new InternalWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://m.mydomain.nl/");
}
}
Sorry to be so stupid, this actualy fixed it. With mailto, there is no host, thus getHost() will return null and causes the exception.
private class InternalWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// This is my web site, so do not override; let my WebView load the page
if (Uri.parse(url).getHost() != null && Uri.parse(url).getHost().equals("m.domain.nl")) {
return false;
} else {
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}

Categories

Resources