C#My programming mistakesSo Android, Much HeadacheTips and TricksXamarin.Android

Working with WebViews in Xamarin.Android

Showing a webpage inside a Xamarin.Android app can be done using a WebView. Here are some tips.

Using Directives

Pop science says that more choices don’t bring more happiness. Well, yeah. Turns out you have two options for creating an URL. This was half of my problem.

Screenshot 2014-01-23 00.02.11

Initially, I was referencing the System library, which contains a Uri type, but not the one required for Monodroid webview. The correct type to use was Android.Net.Uri, but IntelliSense was not going to help me in anyway trying to create the proper Uri type: even after removing the using System directive, these were the only options it gave.

Screenshot 2014-01-23 00.03.17

Screenshot 2014-01-22 23.57.31

I needed to include the using Android.Net directive, and only after everything compiled.

Correctly format the Uri

Remember to include http:// in front of your URL and don’t have any whitespace in it, otherwise it will throw an exception.

No Activity found to handle Intent { act=android.intent.action.VIEW dat= xmonodev.wordpress.com }

Alternatives

There are more ways to open a web page, depending on how much control you want over it. The code provided in this Stackoverflow answer also worked fine for me.

Bonus treat: Hello Mozilla!

After getting the WebView to work, I put in the initial URL I wanted (of my MDN profile) and found this in the WebConsole (just the normal console in Xamarin Studio).

Screenshot 2014-01-18 01.06.02

Leave a Reply

Your email address will not be published. Required fields are marked *