<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ankita Bajaj on Medium]]></title>
        <description><![CDATA[Stories by Ankita Bajaj on Medium]]></description>
        <link>https://medium.com/@ankitabajaj?source=rss-5080fa7b96c7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*u0ZcFjMCsKCFlj9-qKi7yg.jpeg</url>
            <title>Stories by Ankita Bajaj on Medium</title>
            <link>https://medium.com/@ankitabajaj?source=rss-5080fa7b96c7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 23:34:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ankitabajaj/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Leveraging the power of WebApps in Native Android]]></title>
            <link>https://medium.com/sequoia-com/leveraging-the-power-of-webapps-in-native-android-e88387fade6c?source=rss-5080fa7b96c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e88387fade6c</guid>
            <category><![CDATA[pwa]]></category>
            <category><![CDATA[webview]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[progressive-web-app]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ankita Bajaj]]></dc:creator>
            <pubDate>Mon, 13 Jul 2020 02:34:10 GMT</pubDate>
            <atom:updated>2020-07-13T06:31:43.488Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9fOP0eTR62qFl2tWHUnBIw.png" /></figure><h3>Leveraging the power of Web Apps in Native Android</h3><p>When it comes to mobile app development on Android or iOS, there are 3 different approaches: Native app development, Mobile web-app development better known as Progressive Web Apps(PWAs) and Hybrid app development. Among the three options, Native application development is still on top since it benefits from full access to the device’s hardware and its features, like camera, GPS, bluetooth, etc., whereas Progressive Web Apps are web apps that bring a native app-like user experience to cross-platform web applications. PWAs are becoming quite common these days. They are relatively ease to deploy, maintain and backward compatibility is not required. But there are some drawbacks. For eg: PWAs do not have full hardware access on your device. The choice between Progressive Web Apps and Native Android ultimately depends on the app requirements.</p><p>However, there may come a time when you want to utilise an existing web-app to avoid the effort of rewriting the views in native. The web-app integration in native is done via Android’s Webview class. The requirement may vary from simply rendering the web-app inside the native to doing a two-way communication between them. For two-way communication Webview’s support for Javascript and JavaScriptInterface comes into picture and that is the main focus of this blog.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ap7O3Kw-10E6eUN2sZfwiA.png" /></figure><p><em>This blog talks more about Native Android application development and how we can plug in web based components inside the native for faster development iteration. It is aimed at people who are developing apps in Android ecosystem</em></p><p>Following code describes how to achieve the two-way communication. Let’s break it down to simple scenarios and look into each of them with an example.</p><p>Following code describes how to achieve the two-way communication. Let’s break it down to simple scenarios and look into each of them with an example.</p><h4><strong>Scenario 1: Load a new url in Webview on click of a html button using a direct call to JavascriptInterface.</strong></h4><p><em>index.html</em></p><p>Let’s create a sample webpage first. You can add this to the assets folder under main.</p><pre>&lt;html&gt;<br>    &lt;body&gt;</pre><pre>&lt;<strong>input type=&quot;button&quot;</strong> <strong>onclick=&quot;NativeHandler.callApi()&quot;  </strong>style=&quot;height:100px; white-space: normal;text-align:left;&quot; value=&quot;[JavaScript -&gt; Java] Call api and load url&quot; /&gt;&lt;br/&gt;</pre><pre>&lt;<strong>p id=&quot;test&quot; </strong>style=&quot;overflow-wrap:break-word&quot;&gt;Demo App&lt;/<strong>p</strong>&gt;</pre><pre>&lt;/body&gt;<br>&lt;/html&gt;</pre><p><em>xml layout</em></p><pre><em>&lt;?</em>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;<em>?&gt;<br></em>&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    tools:context=&quot;com.sequoia.android.activity.<br>WebviewActivity&quot;&gt;</pre><pre>&lt;WebView<br>        android:id=&quot;@+id/common_web_view&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot; /&gt;</pre><pre>&lt;/RelativeLayout&gt;</pre><p><em>JavascriptInterface</em></p><pre><strong>class </strong>DemoJavaScriptInterface(<strong>val activity</strong>: BaseActivity) {</pre><pre><strong>companion object </strong>{<br>        <strong>private val TAG </strong>= DemoJavaScriptInterface::<strong>class</strong>.<em>java</em>.<em>simpleName<br>    </em>}</pre><pre>@JavascriptInterface<br>    <strong>fun </strong>callApi() {<br>        Log.v(<strong>TAG</strong>, <strong>&quot;call to native&quot;</strong>)<br>        <strong>activity</strong>.runOnUiThread(<strong>{ activity</strong>.callApi() <strong>}</strong>)<br>    }<br>}</pre><p><em>WebviewActivity</em></p><pre><strong>class </strong>WebviewActivity : BaseActivity() {</pre><pre><strong>companion object </strong>{<br>    <strong>private val TAG </strong>= WebviewActivity::<strong>class</strong>.<em>java</em>.<em>simpleName<br></em>}<br>    <br><strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {<br>        <strong>super</strong>.onCreate(savedInstanceState)<br>        setContentView(R.layout.<em>webview_activity</em>)<br>   common_web_view.addJavascriptInterface(DemoJavaScriptInterface(<strong>this</strong>), <strong>&quot;NativeHandler&quot;</strong>)<br>        initWebViewSettingsAndClient(<strong>&quot;file:///android_asset/index.html&quot;</strong>)<br>    }</pre><pre>@SuppressLint(<strong>&quot;SetJavaScriptEnabled&quot;</strong>)<br>    <strong>private fun </strong>initWebViewSettingsAndClient(url: String) {<br>        <strong>if </strong>(<em>isDestroyed </em>|| <em>isFinishing</em>) <strong>return<br>        val </strong>settings: WebSettings = common_web_view.getSettings()<br>        settings.<em>javaScriptEnabled </em>= <strong>true<br>        </strong>settings.<em>javaScriptCanOpenWindowsAutomatically </em>= <strong>true<br>        </strong>common_web_view.setWebChromeClient(<strong>object </strong>: WebChromeClient() {<br>            <strong>override fun </strong>onConsoleMessage(consoleMessage: ConsoleMessage): Boolean {<br>                Log.d(<strong>TAG</strong>, consoleMessage.message() + <strong>&quot; -- From line &quot;<br>                        </strong>+ consoleMessage.lineNumber() + <strong>&quot; of &quot;<br>                        </strong>+ consoleMessage.sourceId())<br>                <strong>return super</strong>.onConsoleMessage(consoleMessage)<br>            }<br>        })<br>        common_web_view.loadUrl(url)<br>    }</pre><pre><strong>override fun </strong>callApi() {<br>        <strong>val </strong>url = <strong>&quot;https://github.com&quot;<br>        </strong>loadHtmlInWebView(url)<br>    }</pre><pre>@SuppressLint(<strong>&quot;SetJavaScriptEnabled&quot;</strong>)<br>    <strong>private fun </strong>loadHtmlInWebView(url: String) {<br>        common_web_view.loadUrl(url)<br>    }<br>}</pre><p>Here on click of button in html creates a call to <em>callAPi</em> function in JavascriptInterface which we had set with <em>addJavascriptInterface</em> to the Webview in activity. We also need to set <em>javaScriptEnabled </em>to true explicitly on the Webview.</p><h4><strong>Scenario 2:</strong> Load a new url in Webview on click of a html button using an indirect call to JavascriptInterface via script function in html.</h4><p><em>index.html</em></p><pre>&lt;html&gt;<br>    &lt;head&gt;<br>        &lt;<strong>script type=&quot;text/javascript&quot;</strong>&gt;</pre><pre>function testSync() {<br>                NativeHandler.callApi();<br>            }<br>&lt;/script&gt;<br>&lt;/head&gt;</pre><pre>&lt;body&gt;<br>    <br>    &lt;<strong>input type=&quot;button&quot; onclick=&quot;testSync()&quot;  </strong>style=&quot;height:100px; white-space: normal;text-align:left;<strong>&quot; </strong>value=&quot;[Sync JavaScript -&gt; Java] Call api and load url&quot; /&gt;&lt;br/&gt;</pre><pre>&lt;<strong>p id=&quot;test&quot; </strong>style=&quot;overflow-wrap:break-word<strong>&quot;</strong>&gt;Demo App&lt;/<strong>p</strong>&gt;</pre><pre>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>Here the only thing that changes is the on click handling in html. We now call a script function to call the <em>callApi</em> method instead of calling it directly.</p><h4><strong>Scenario 3:</strong> Modifying data in Webview- Change text on click of a html button</h4><p><em>index.html</em></p><p>To the above html just add the following script function</p><pre>function setUrlInText(url) {<br>   if(typeof(url) == &#39;string&#39;) {<br>        document.getElementById(&quot;test&quot;).innerHTML = url;<br>        console.log(&quot;setUrlInText function entered&quot;);<br>     }<br> }</pre><p>This function will be called by the Webview to set new text in html.</p><p><em>WebviewActivity</em></p><p>Modify the <em>loadHtmlInWebView </em>method to call the new script function <em>setUrlInText</em> and pass new url as parameter.</p><pre>@SuppressLint(<strong>&quot;SetJavaScriptEnabled&quot;</strong>)<br><strong>private fun </strong>loadHtmlInWebView(url: String) {<br><em>    </em>common_web_view.evaluateJavascript(<strong>&quot;setUrlInText(&#39;&quot;</strong>+ url+<strong>&quot;&#39;)&quot;</strong>, <strong>null</strong>)<br>}</pre><p>This will set the new text in the particular elementId. Here we are passing null in the callback. <em>Scenario5</em> demonstrates how to handle javascript callbacks.</p><h4><strong>Scenario 4:</strong> Set the data in native and pass the entity to Webview and display its properties in html.</h4><p><em>UserDetails</em></p><p>Let’s create a simple data class of UserDetails with name, email and phoneNumber property.</p><pre><strong>data class </strong>UserDetails(<strong>val name</strong>: String, <strong>val email</strong>: String, <strong>val phoneNumber</strong>: String)</pre><p><em>index.html</em></p><p>To the above html just add the following script function</p><pre>function getData(userDetails) {<br>   if(typeof(userDetails) == &#39;object&#39;) {<br>       var name = userDetails.name || &#39;&#39;;<br>       var phoneNumber = userDetails.phoneNumber || &#39;&#39;;<br>       var email = userDetails.email || &#39;&#39;;<br>       document.getElementById(&quot;test&quot;).innerHTML = &quot;name = &quot; + name + &quot;, &quot; +<br>                                                   &quot;email = &quot; + email + &quot;, &quot; +<br>                                                   &quot;phoneNumber = &quot; + phoneNumber;<br>       console.log(&quot;getData function exiting &quot; + name + &quot;, &quot; + email + &quot; , &quot; + phoneNumber);<br>    }<br>}</pre><p>This will set the <em>UserDetails</em> in the particular elementId. Here we are passing null in the callback.</p><p><em>xml layout</em></p><p>In xml layout add 3 fields to input data and a button to send data to Webview.</p><pre><em>&lt;?</em>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;<em>?&gt;<br></em>&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    tools:context=&quot;com.sequoia.android.activity.<br>WebviewActivity&quot;&gt;</pre><pre>&lt;LinearLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;<br>        android:orientation=&quot;vertical&quot;&gt;</pre><pre>&lt;LinearLayout<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:orientation=&quot;vertical&quot;&gt;</pre><pre>&lt;EditText<br>                android:id=&quot;@+id/etName&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:ems=&quot;10&quot;<br>                android:hint=&quot;Enter Name&quot;<br>                android:inputType=&quot;textPersonName&quot; /&gt;</pre><pre>&lt;EditText<br>                android:id=&quot;@+id/etEmail&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:ems=&quot;10&quot;<br>                android:hint=&quot;Enter Email&quot;<br>                android:inputType=&quot;textEmailAddress&quot; /&gt;</pre><pre>&lt;EditText<br>                android:id=&quot;@+id/etPhone&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:ems=&quot;10&quot;<br>                android:hint=&quot;Enter Phone Number&quot;<br>                android:inputType=&quot;phone&quot; /&gt;</pre><pre>&lt;Button<br>                android:id=&quot;@+id/sendData&quot;<br>                android:layout_width=&quot;wrap_content&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:onClick=&quot;sendDataToWebview&quot;<br>                android:text=&quot;Send data to webview&quot; /&gt;<br>        &lt;/LinearLayout&gt;</pre><pre>&lt;WebView<br>            android:id=&quot;@+id/common_web_view&quot;<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot; /&gt;<br>    &lt;/LinearLayout&gt;<br>&lt;/RelativeLayout&gt;</pre><p><em>WebviewActivity</em></p><p>In the activity add the following function to handle on click of native button.</p><pre><strong>fun </strong>sendDataToWebview(view: View) {<br>    <strong>val </strong>name = etName.<em>text</em>.toString()<br>    <strong>val </strong>email = etEmail.<em>text</em>.toString()<br>    <strong>val </strong>phone = etPhone.<em>text</em>.toString()<br>    <strong>val </strong>userDetails = UserDetails(name, email, phone)</pre><pre><strong>val </strong>strUserDetails: String = Gson().toJson(userDetails)<br>    common_web_view.evaluateJavascript(<br>        <strong>&quot;getData($</strong>strUserDetails<strong>)&quot;</strong>, <strong>null<br>    </strong>)<br>}</pre><p><em>evaluateJavascript</em> is used to call the html script function <em>getData</em> and passes the stringified object to its params.</p><h4><strong>Scenario 5:</strong> Modify data received in html and send it back to native and display it.</h4><p><em>index.html</em></p><p>Add the following code to the <em>getData</em> script function. We’ll now return a modified <em>UserDetails</em> object.</p><pre>userDetails.name = &#39;Demo App&#39;;<br>userDetails.email = &#39;demo@google.com&#39;;<br>userDetails.phoneNumber = 789;<br>return userDetails;</pre><p><em>WebviewActivity</em></p><p>Add the following code to <em>sendDataToWebview</em> function.</p><pre>common_web_view.evaluateJavascript(<br>    <strong>&quot;getData($</strong>strUserDetails<strong>)&quot;</strong>, <strong>object </strong>: ValueCallback&lt;String&gt; {<br>        <strong>override fun </strong>onReceiveValue(value: String?) {<br>            <strong>val </strong>userDetails = Gson().fromJson(value, UserDetails::<strong>class</strong>.<em>java</em>)<br>            etName.setText(userDetails.<strong>name</strong>)<br>            etEmail.setText(userDetails.<strong>email</strong>)<br>            etPhone.setText(userDetails.<strong>phoneNumber</strong>)<br>            Log.e(<strong>TAG</strong>, value)<br>        }<br>    })</pre><p>We now pass a callback value to <em>evaluateJavascript</em> method and data from script function is received in <em>onReceiveValue</em>. We use this object to set new data in native view.</p><p>In this blog we have looked at a few examples which cover most of the implementation details of Native and Web-App communication. The use cases covered here were limited but you can extend these further to cover any complex requirement that you may have.</p><p>You can view the complete code for this article <a href="https://github.com/SequoiaConsulting/Android-OpenSource/tree/master/JavascriptWebviewDemo">here</a>.</p><p>Feel free to reach out to me incase you have any queries or have feedback on this post — ankita.bajaj@sequoia.com</p><p>Here is my profile on <a href="https://www.linkedin.com/in/bajajankita/">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e88387fade6c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sequoia-com/leveraging-the-power-of-webapps-in-native-android-e88387fade6c">Leveraging the power of WebApps in Native Android</a> was originally published in <a href="https://medium.com/sequoia-com">Sequoia.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>