The PSLab Android App allows users to perform experiments using the PSLab device. The experience to perform an experiment should resemble the generic way to perform the experiment. So we associated an Experiment Doc file which the user can refer to while performing experiment. Just like a regular lab manual, the experiment doc contains the AIM, THEORY & FORMULAS, SCHEMATIC, OUTPUT, etc. In the PSLab Desktop App, since there was already a provision for using HTML docs and so I avoided reinventing the wheel and used those html files as it is.
The problem we faced was how to open a bunch of HTML files with their corresponding CSS, JS files in Android’s webView.
There are two ways it can be done:
- Host the experiment docs on a server and make a request from the android app for the specific experiment doc like Diode I-V, Zener I-V, etc.
- Put the folder containing all html, CSS, js files in assets folder and request for the HTML doc files locally.
The PSLab developer team went with the second option as the availability of Internet is necessary for the performing experiment if we follow the first option and so to avoid this dependence on the Internet, we went with the second option and stored HTML docs locally in assets folder.
- Put the folder containing all the HTML, CSS, JS files in the assets folder in your android project. In this case the folder is DOC_HTML.
- Define the WebView in xml and take the webView’s reference in your activity/fragment
<WebView android:id="@+id/perform_experiment_wv" android:layout_width="match_parent" android:layout_height="match_parent" />
webView = (WebView) view.findViewById(R.id.perform_experiment_wv);
- Load the url in webView in the format as shown below
webView.loadUrl("file:///android_asset/DOC_HTML/apps/" + htmlFile);
“file:///” acts as resource identifier, so “file:///android_asset/” actually points to “pslab-android/app/src/main/assets/”.
From the assets directory, we can a provide route to any HTML file. Here I put all HTML files in apps folder and used the string variable “htmlFile” to point to the specific html file.
Similarly html files stored in the external storage can also be accessed but there are some cases you need to handle. For example,if external storage is mounted, you can’t request the html file from external storage.
To request html files from external storage, make sure that you have the following permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
Relative to baseDir you can specify the path from your html files, like
baseDir + “DOC_HTML/apps” + htmlFile
Putting HTML files in the assets folder and requesting it by webView’s loadURL() method is the best but there are various drawbacks of using this method like the increase in size of the apk. In our case, the normal apk size was 3MB but after adding the html doc folder it increased to 7MB. It increased by almost an additional size of the html folder added in assets. As it’s written, in the android’s project overview guide, the assets folder contains files that should be compiled into an .apk file as-is.