Close
Topic : Using Android Assets - Using Android Assets:

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.



Simple web page : infobrother

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.



  • 1: Open a New Project:

    1 » Open Visual studio, and create new project.
    2 » Select a Blank android app from the template.
    3 » Give it a proper name, browse your location and click Ok.



step to Edit Main.axml file: infobrother
  • 2: Steps to Edit Main.axml File:

    1 » From Solution Explorer Tab, Open Resources » Layout » Main.axml:
    2 » Using Toolbox, Drag and Drop WebView in Designer Window.
    3 » Click at Source button to get into the XML coding File.




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>


steps to include assets: infobrother
  • 3: Steps to Include Assets:

    1 » In Solution Explorer Tab, Right click an Assets and Add Existing file.
    2 » Select your All files and click at add button to include the files in assets.




  • 4: Steps to Edit MainActivity.cs File:

    1 » In Solution Explorer tab, Open MainActivity.css file to edit.
    2 » At the top, include Webkit Namespace.

    using Android.Webkit;
    

    3 » Copy the given code, and replace it with your existing code in MainActicity.cs file



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");
        }
    }
}


Run Emulator: infobrother
  • 5: Run Emulator:

    1 » Now our project is ready. Just start the emulator. and check out the result.
    2 » You will see the web page on your Emulator screen.




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.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement