Assets:
Assets provide a way to include arbitrary files Like text, xml, fonts, Music, and Video in Our Application. If we try to Include these files as Resources Android will Process them into it resource system and we will not be able to get the Raw data. If we want to access data untouched, Assets are One way to do it.
Add Asset to Project:
We can include HTML, xml, music, video files in our project using Android assets. in this tutorial, we will learn how we can use asset to display a local webpage. We have a simple html web page, including Image, and CSS. we will include these files in our project using Android Assets. And the end, we will display the web page in our emulator using WebView. follow the given steps to make this project.
WebView:
WebView is the View that displays web pages. It is the class basic Upon which we can roll our own browser or simply display some online content with in our Activity. This class use Webkit Rendering engine to display web pages and perform different operations on webpages including Navigate forward and backward, zoom in and out, perform text search, browsing, and more.
In Main.axml file, You will get the following code. And No need to change it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/webView1" />
</LinearLayout>
This is Our main file, we will discuss it in our next tutorials, For Now, just copy the given code and replace it with your code in MainActivity.cs File.
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Webkit;
namespace WebsiteView
{
[Activity(Label = "WebsiteView", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
WebView websiteview = FindViewById<WebView>(Resource.Id.webView1);
websiteview.LoadUrl("file:///android_asset/webview.html");
}
}
}
In this tutorial, we create a web view Application using Android Assets. this tutorial was just about android assets, we discuss, what is android assets, and how to use it. in our next upcoming tutorials, we will discuss it in deep.