0

Android.Webkit.WebView not load url

I Created a method in main activity class this method have a WebView but not load url

public void LaunchBrowserView(string authorizationServerUrl)
        {
            try
            {
                var web_view = new Android.Webkit.WebView(this);

                web_view.Settings.JavaScriptEnabled = true;
                web_view.Settings.DomStorageEnabled = true;
                //web_view.Settings.= true;
                //web_view.Settings.AllowContentAccess = true;
                web_view.SetWebViewClient(new MyBrowser());
                web_view.Settings.LoadsImagesAutomatically = true;
                web_view.LoadUrl(authorizationServerUrl);
            }
            catch (System.Exception ex)
            {
            }
        }
class MyBrowser : WebViewClient
    {
        override public bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, string url)
        {
            view.LoadUrl(url);
            return false;
        }

        public override void OnReceivedSslError(Android.Webkit.WebView view, SslErrorHandler handler, SslError error)
        {
            base.OnReceivedSslError(view, handler, error);
        }

    }
15
  • Why exactly do you need a static variable exactly? Commented Oct 21, 2019 at 13:37
  • Hello @FreakyAli i edited my question please see. Commented Oct 21, 2019 at 13:40
  • This seems to look like a service. So are you creating this service in your MainActivity itself? Also, why do you need your WebView in the service anyway? Also don't you think doing what you are doing would create a memory leak since your activity would never be disposed? Commented Oct 21, 2019 at 13:42
  • i am loading a web url Commented Oct 21, 2019 at 13:43
  • loading a URL in the service? why do you need that I mean I am confused! Commented Oct 21, 2019 at 13:44

1 Answer 1

1

I test the code you provided, and invoke on my own, it works well.

activity_main.xml

<android.webkit.WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);


        LaunchBrowserView("https://www.google.com");
    }

Result: enter image description here

I have upload on GitHub, you could download from WebView folder for reference. https://github.com/WendyZang/Test.git

Updated:

If you want to do this without axml file, you could create the layout in activity.

 protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        //SetContentView(Resource.Layout.activity_main);
        RelativeLayout layout_main = new RelativeLayout(this);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);
        layout_main.LayoutParameters = layoutParams;

        WebView webView = new WebView(this);
        webView.LayoutParameters = layoutParams;

        layout_main.AddView(webView);
        SetContentView(layout_main);
        LaunchBrowserView("https://www.google.com", webView);
    }
    public void LaunchBrowserView(string authorizationServerUrl, WebView webView)
    {
        try
        {
            webView.Settings.JavaScriptEnabled = true;
            webView.Settings.DomStorageEnabled = true;
            //web_view.Settings.= true;
            //web_view.Settings.AllowContentAccess = true;
            webView.SetWebViewClient(new MyBrowser());
            webView.Settings.LoadsImagesAutomatically = true;
            webView.LoadUrl(authorizationServerUrl);
        }
        catch (System.Exception ex)
        {
        }

Result:

enter image description here

I have upload on GitHub, download from WebViewDemo2 for reference. https://github.com/WendyZang/Test.git

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

12 Comments

Hello @WendyZang in your solution you used var webView = FindViewById<WebView>(Resource.Id.webView); but i am not using axml file any other way to declare webview
I have upadted the way without axml, you could download from github.
Hello @WendyZang i am not set SetContentView(xml) i am open Xaml page via this LoadApplication(new App());
@Deepakyogi What is the xml? LoadApplication(new App()); is used to call LoadApplication with an instance of the App class, it is not used to open xaml page.
Without use of SetContentView(layout_main) possible load url via WebView in this way ` WebView webView = new WebView(this); webView.LoadUrl(authorizationServerUrl); `
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.