I need a method where in when I click a YouTube video in my web view , I need it to perform some action , i.e just get the URL of that video link .
So , how do i get that on click on that video ?
Let's assume video extension is .mp4
In WebViewClient, check for the url with such extension. E.g.
public class myWebClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(".mp4"))
{
downloadVideo(url);
}
else
{
view.loadUrl(url);
}
Log.e(TAG, url);
return true;
}
}
Related
can you please tell me whats wrong here? i want open my website and facebook (Facebook logging plugin) in webview only. all other external link will open in other external browsers. below code allow facebook to open in external browser.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (!url.contains ("https://kroybook.com/')'https://facebook.com/")
{
Uri uri = Uri.parse(url);
startActivity(Intent.createChooser(new Intent(Intent.ACTION_VIEW, uri), "Choose browser"));
CookieManager.getInstance().setAcceptCookie(true);
} else {
myWebView.loadUrl(url);
return true;
}
return false;
}
This the code i used
public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url != null && url.startsWith("whatsapp://")) { view.getContext().startActivity( new Intent(Intent.ACTION_VIEW, Uri.parse(url))); return true; } else { return false; } }
I am doing a project in xamarin.
I exactly needed a popup that shows all the music apps installed in my phone on a button click, so user can select anyone of them and play music independently.
I am able to do this in android with music intent.
This is like I want to play an mp3 file and I tapped on a button. The iOS system shows all the available music in the sheet and by choosing one of them I can play that mp3 file.
Music intent ios
First you declare an interface to openApp:
public interface IOpenAppService
{
Task<bool> Launch(string stringUri);
}
then, in your customRenderer:
[assembly: Xamarin.Forms.Dependency(typeof(OpenAppService))]
namespace OpenAppLaunch.iOS
{
public class OpenAppService : IOpenAppService
{
public Task<bool> Launch(string stringUri)
{
try
{
NSUrl request = new NSUrl(stringUrl);
bool isOpened = UIApplication.SharedApplication.OpenUrl(stringUrl);
if (isOpened == false)
UIApplication.SharedApplication.OpenUrl(new NSUrl(stringUrl));
return Task.FromResult(true);
}
catch (Exception ex)
{
Debug.WriteLine("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
var alertView = new UIAlertView("Error", ex.Message, null, "OK", null);
alertView.Show();
return Task.FromResult(false);
}
}
}
You can use it by calling OpenAppService.Launch("skype://");
I'm making an app in Xamarin android where user navigates. The app contains webview. When a user opens webview, url gets loaded and browsing can be done. When he ends the app and opens it again, URL is loaded again instead of viewing last visited URL.
I don't know what I'm doing wrong here.
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
webView = FindViewById<WebView>(Resource.Id.webView1);
webView.SetWebViewClient(new MyWebClient());
CookieManager.Instance.SetAcceptCookie(true);
webView.Settings.JavaScriptEnabled = true;
webView.Settings.SetAppCacheEnabled(true);
webView.LoadUrl(getUrl());
webView.SetPadding(0, 0, 0, 0);
webView.Settings.SetSupportZoom(true);
}
public void saveUrl(String url)
{
ISharedPreferences sp = GetSharedPreferences("SP_WEBVIEW_PREFS", FileCreationMode.Private);
ISharedPreferencesEditor editor = sp.Edit();
editor.PutString("SAVED_URL", url);
editor.Commit();
}
public String getUrl()
{
ISharedPreferences sp = GetSharedPreferences("SP_WEBVIEW_PREFS", FileCreationMode.Private);
//If you haven't saved the url before, the default value will be google's page
return sp.GetString("SAVED_URL", "http://google.com");
}
public void onPageFinished(WebView view, String url)
{
this.onPageFinished(view, url);
saveUrl(url);
}
}
internal class MyWebClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
view.LoadUrl(url);
return false;
}
}
You have placed onPageFinished method in an Activity. It should be overridden in MyWebClient class as below:
internal class MyWebClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading(WebView view, string url)
{
view.LoadUrl(url);
return false;
}
public override void OnPageFinished(WebView view, String url)
{
base.OnPageFinished(view, url);
//Save the url here.
//This method itself gives you the last url loaded as it's url Parameter.
ISharedPreferences sp = Application.Context.GetSharedPreferences("SP_WEBVIEW_PREFS", FileCreationMode.Private);
ISharedPreferencesEditor editor = sp.Edit();
editor.PutString("SAVED_URL", url);
editor.Commit().
}
}
This method will be automatically called when URL finished loading and then you will store your loaded URL in this method.
I created app with webview, and i want load all internal links in webview and load external links in android browser. Now problem is I am using html ads and when i click on ads i want open external browser, but its opening in webview. only problem with ads otherwise everything is works fine. So how can i do this?
My code is below:
`class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) { if (Uri.parse(url).getHost().equals("www.mysite.com")) {
view.loadUrl(url);
return true;
}else{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;}}`
You code should be:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.mysite.com")) {
return true;
}else{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return false;
}
}
All I changed was:
1.) Returning true loads the URL in the webview, no need for view.loadUrl()
2.) Return false when you broadcast the ACTION_VIEW intent
I did the some modifications and it is working perfectly for banner ads.
I have made following changes:
changed if condition.
I am returning false for 'if' block as documentation says:
If WebViewClient is provided, return true means the host application
handles the url, while return false means the current WebView handles
the url
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.contains("www.mysite.com"))
{
view.loadUrl(url);
return false;
}else
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
I want to check if a given URL exists and it's an image, in order to create a new Image(String url) from it. If the given URL is not an image then it should return an error.
I was looking for the same thing - I wanted to determine when image is not loaded from URL. There is an ErrorHandler for this purpose. Here's the code:
Image img = new Image("some_url/img.jpg");
img.addErrorHandler(new ErrorHandler() {
#Override
public void onError(ErrorEvent event) {
System.out.println("Error - image not loaded.");
}
});
You could do this with a RequestBuilder -- just request the image URL, use the Response's getHeaders() method to get the content type, and check if it's an image.
Image img = new Image(); //no url parameter
img.addErrorHandler(new ErrorHandler() {
#Override
public void onError(ErrorEvent event) {
System.out.println("Error - image not loaded.");
}
});
img.setUrl("some_url/img.jpg"); // set the url after handler