Made Web browser in Xamarin

Like .net we make our own web browser in windows application we can also make web browser for android tab/phone. To do this we use Web View class.

By using web view we can run a link on default browser as my previous post “Start Xamarin Application on Android Boot or Tart up ” and we can create a web browser.

Your Android Manifest file should be like this.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android” android:versionCode=”1″ android:versionName=”1.0″ package=”webopenapp.webopenapp”>
<uses-sdk />
<application android:label=”webopenapp”>
</application>

<uses-sdk android:minSdkVersion=”4″ />
<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
<uses-permission android:name=”android.permission.RECEIVE_BOOT_COMPLETED” />
</manifest>

now copy this code in your Layout file that is Main.axml

<?xml version=”1.0″ encoding=”utf-8″?>
<WebView xmlns:android=”http://schemas.android.com/apk/res/android”
android:id=”@+id/webview”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” />

In MainActivity.cs you can use this

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Webkit;

namespace webbrowser
{
[Activity (Label = “webbrowser”, MainLauncher = true, Theme = “@android:style/Theme.NoTitleBar”)]
public class MainActivity : Activity
{
WebView web_view;
//int count = 1;

protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

// Set our view from the “main” layout resource
SetContentView (Resource.Layout.Main);

web_view = FindViewById<WebView> (Resource.Id.webview);
web_view.Settings.JavaScriptEnabled = true;
web_view.Settings.JavaScriptEnabled = true;
web_view.Settings.SetSupportZoom(true);
web_view.Settings.BuiltInZoomControls = true;
web_view.Settings.LoadWithOverviewMode = true; //Load 100% zoomed out
web_view.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
web_view.ScrollbarFadingEnabled = true;
web_view.LoadUrl (“http://www.technodoulia.com”);

web_view.SetWebViewClient (new HelloWebViewClient ());
// Get our button from the layout resource,
// and attach an event to it
//Button button = FindViewById<Button> (Resource.Id.myButton);

//button.Click += delegate {
// button.Text = string.Format (“{0} clicks!”, count++);
//};
}
private class HelloWebViewClient : WebViewClient
{
public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
view.LoadUrl (url);
return true;
}
}
public override bool OnKeyDown (Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
if (keyCode == Keycode.Back && web_view.CanGoBack ()) {
web_view.GoBack ();
return true;
}

return base.OnKeyDown (keyCode, e);
}
}
}

 

Leave a Reply