Visual Studio 2015 Xamarian Webview app with small apk

Creating a small app on Visual Studio was such a headache. However i did this. Here is steps to make a simple app.

1. Custom Install Visual Studio with Xamarian Mobile Development tool. Install all.

2. Go To C:\Program Files (x86)\Android\android-sdk and from sdk manager install latest android version, oldest version like 2.2 and some other version 4.4 . Give proxy internet to sdk manager if required from settings.

3. Go to Tools > Option > Xamarian and give path of android sdk as shown.




Now you are ready to make your first android app on Visual Studio 2015.

I will make a simple webview app which will open a url. This website is bootstrap and looks like app so no android coding required.

Start new Project select Blank Android App.



  • Create a new project named HelloWebView.
  • Open the Resources\Layout\Main.axml file and insert the following:
  • <?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"
    />

    1. Now open the Activity1.cs file. Add a using directive for Webkit:
      using Android.Webkit;
      At the top of the class, declare a WebView object:
      WebView web_view;
      Then use the following code for the OnCreate() method:
      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.LoadUrl ("http://www.google.com");
      }
      This initializes the member WebView with the one from the Activity layout and enables JavaScript for the WebView with JavaScriptEnabled = true (see the Call C# from JavaScript recipe for information about how to call C# functions from JavaScript). Finally, an initial web page is loaded with LoadUrl(String).
    2. Because this application needs access to the Internet, you need to add the appropriate permissions to the Android manifest file. Bring up your project's properties with Project -> HelloWebView Properties.  This can be done is Xamarin Studio by right clicking on the project and going to Options -> Android Application. Since there is no default manifest file, click the link to add one:
    3. Final mainactivity.cs will look like:
      using Android.App;
      using Android.Widget;
      using Android.OS;
      using Android.Webkit;
      namespace NTPC1
      {
          [Activity(Label = "NTPC1", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.NoTitleBar")]
          public class MainActivity : Activity
          {
              private WebView web_view;
              protected override void OnCreate(Bundle bundle)
              {
                  base.OnCreate(bundle);
      
                  // Set our view from the "main" layout resource
                   SetContentView (Resource.Layout.Main);
                  web_view = FindViewById(Resource.Id.webview);
                  web_view.Settings.JavaScriptEnabled = true;
                  string customHtml = "

      Hello, WebView

      " + "

      Heading 1

      Heading 2

      Heading 3

      " + "This is a sample paragraph. " + ""; //web_view.LoadData(customHtml, "text/html", "UTF-8"); //web_view.SetWebViewClient(new WebViewClient()); web_view.LoadUrl("http://62.92.122.146/Default.aspx"); web_view.SetWebViewClient(new HelloWebViewClient()); } public class HelloWebViewClient : WebViewClient { public override bool ShouldOverrideUrlLoading(WebView view, string url) { view.LoadUrl(url); return true; } } } }
    Now you will get problem in running this app on emulators. Which are deadly slow. So what workaround i did was , Just build the solution and from debug directory get the *.signed.akp emailed to your android device and install on fone to test. Its much faster. Steps to build is as follows.

    Here are the steps to generate small apk for Visual Studio 2015 Xamarian Webview app, this will run most of the android phones.



    v7a will supported by most of the device

     This will be going to reduce your app size from 14MB to 4 MB.
    Generate 1 apk will create multiple apk for cpu architecture.


    Written By: Vinod Kotiya

    Comments