Create the WebViewApplixir class (this class will completely load and handle the webview ) :
JAVA:
import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import androidx.annotation.NonNull;
public class WebViewApplixir extends WebView {
private String webUrl;
private Context context;
public WebViewApplixir(@NonNull Context context,String webUrl) {
super(context);
this.context=context;
this.webUrl=webUrl; // your website URL
WebSettings webSettings = this.getSettings();
webSettings.setJavaScriptEnabled(true);
this.setWebViewClient(new WebViewClient());
}
public void showAds(OnCompleteAdListener onCompleteAdListener){
this.loadUrl(webUrl);
this.addJavascriptInterface(new Applixir(context){
@JavascriptInterface
@Override
public void adStatusCallback(String status) {
super.adStatusCallback(status);
onCompleteAdListener.statusCallBack(status);
}
} ,"Android");
}
public interface OnCompleteAdListener {
void statusCallBack(String status);
}
public class Applixir{
Context mContext;
public Applixir(Context c) {
mContext = c;
}
@JavascriptInterface
public void adStatusCallback(String status) {
}
}
}
KOTLIN:
import android.content.Context
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
class WebViewApplixirKotlin( context: Context, private val webUrl: String) :
WebView(context) {
fun showAds(onCompleteAdListener:OnCompleteAdListener) {
this.loadUrl(webUrl)
addJavascriptInterface(object : Applixir(context) {
@JavascriptInterface
override fun adStatusCallback(status: String?) {
super.adStatusCallback(status)
onCompleteAdListener.statusCallBack(status)
}
}, "Android")
}
interface OnCompleteAdListener {
fun statusCallBack(status: String?)
}
open inner class Applixir(var mContext: Context) {
@JavascriptInterface
open fun adStatusCallback(status: String?) {
}
}
init {
val webSettings = this.settings
webSettings.javaScriptEnabled = true
}
}