Deepak yogi
Deepak yogi

Reputation: 117

Android.Webkit.WebView not load url in Xamarin.Android

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);
        }

    }

Upvotes: 0

Views: 2012

Answers (1)

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10958

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

Upvotes: 1

Related Questions