Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Updated 31 August 2022
In this article, we are going to learn how we can call android functions using javascript and vice versa using the javascript interface for android webview.
To know about webview and how it works, head over here.
For javascript to work in our webview, we need to enable it using webview settings.
Through an interface, WebView enables us to connect JavaScript code to Android code.
The class that provides the interface for JS must be passed, along with the name that will be used to show the instance in JS (for example, “Android”).
As a result, an interface called Android will be created for JavaScript that is running in WebView.
By using the interface we can achieve the following:
Create a JavaScript Interface Class
Note: @JavascriptInterface annotation is required for API 17 and above.
Then install this interface using the following code.
Let’s create an HTML file where we will use our Java/Kotlin functions
Output:
Calling JS Function from Java/Kotlin
MainActivity’s code
activity_main.xml
This is it, hope you have understood how we can use an interface to communicate with our JS code and vice versa.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.