1

How do I open a website with Mono for Android? I assume that I need to use an Intent, but I don't know which one.

2 Answers 2

4
var intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse("http://www.stackoverflow.com"));

StartActivity(intent);
2

Another possibility is to create a WebView and load an URL there, this way you can have some more control over how it looks and how it reacts to things such as Javascript.

You could create your own activity somewhat like this:

using System;

using Android.App;
using Android.OS;
using Android.Webkit;
using Android.Views;

namespace WebViewSample
{
    [Activity(Label = "MyAwesomeWebActivity", MainLauncher = true, Icon = "@drawable/icon")]
    public class MyAwesomeWebActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            WebView webView = new WebView(this);
            webView.Settings.JavaScriptEnabled = true;
            webView.Settings.SetSupportZoom(true);
            webView.Settings.BuiltInZoomControls = true;
            webView.Settings.LoadWithOverviewMode = true; //Load 100% zoomed out
            webView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
            webView.ScrollbarFadingEnabled = true;


            webView.VerticalScrollBarEnabled = true;
            webView.HorizontalScrollBarEnabled = true;

            webView.SetWebViewClient(new AwesomeWebClient());
            webView.SetWebChromeClient(new AwesomeWebChromeClient(this));

            AddContentView(webView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent));

            webView.LoadUrl("http://stackoverflow.com");
        }

        private class AwesomeWebClient : WebViewClient { }

        private class AwesomeWebChromeClient : WebChromeClient
        {
            private Activity mParentActivity;
            private string mTitle;

            public AwesomeWebChromeClient(Activity parentActivity)
            {
                mParentActivity = parentActivity;
                mTitle = parentActivity.Title;
            }

            public override void OnProgressChanged(WebView view, int newProgress)
            {
                mParentActivity.Title = string.Format("Loading {0}%", newProgress);
                mParentActivity.SetProgress(newProgress * 100);

                if (newProgress == 100) mParentActivity.Title = mTitle;
            }
        }
    }
}

You have a ton of possibilities here.

1
  • Just showing off some of the possibilities WebView gives you. You could simple leave out the code you don't need. Commented Jan 10, 2012 at 15:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.