This is a very powerful way to implement java script into Android web view component using JavascriptInterface. You can easily invoke an android method from a WebView.
Let say :
There is a WebView ....
WebView web = new WebView();
Now we need to make it js enabled ...
WebSettings settings = web.getSettings();
settings.setJavaScriptEnabled(true);
Now , need to point it to our custom JSInterface class ..
web.addJavascriptInterface(new JSInterface(), "jsinterface");
Now, load a custom html to load ...
String html = new String();
html = ("<html><center><img border=\"0\" onClick=\"window.jsinterface.specialClick('" + parameter_1 + "','" + parameter_2 + "');\" " + " width='60dip' src=\"" + mediaSource + "\"></html>");
web.loadData(html, "text/html", "utf-8");
This html will load a image fetching from either a url or file ( mediaSource ) and we want to track the onClick method of WebView in the Android code itself. So, there is a method in the JSInterface called specialClick() which will do it. And to invoke the method into HTML code , use the code ..
onClick="window.jsinterface.specialClick('" + parameter_1 + "','" + parameter_2 + "');
Now, the time to write the JSInterface class and specialClick() method.
Create a class named JSInterface and put the code ...
public class JSInterface
{
public void specialClick(String param1, String param2)
{
Log.d("CZ","param1..." + param1 + " , param2..." + param2);
handler.post(new Runnable()
{
public void run()
{
}
});
}
}
In such way , whatever parameters you have passed from HTML onClick method , you will get it in Android code. So, connection between WebView javascript and Android native code is done.
For, further clarification and doubts , you may contact me .... surojit.pakira@collectivezen.com
Let say :
There is a WebView ....
WebView web = new WebView();
Now we need to make it js enabled ...
WebSettings settings = web.getSettings();
settings.setJavaScriptEnabled(true);
Now , need to point it to our custom JSInterface class ..
web.addJavascriptInterface(new JSInterface(), "jsinterface");
Now, load a custom html to load ...
String html = new String();
html = ("<html><center><img border=\"0\" onClick=\"window.jsinterface.specialClick('" + parameter_1 + "','" + parameter_2 + "');\" " + " width='60dip' src=\"" + mediaSource + "\"></html>");
web.loadData(html, "text/html", "utf-8");
This html will load a image fetching from either a url or file ( mediaSource ) and we want to track the onClick method of WebView in the Android code itself. So, there is a method in the JSInterface called specialClick() which will do it. And to invoke the method into HTML code , use the code ..
onClick="window.jsinterface.specialClick('" + parameter_1 + "','" + parameter_2 + "');
Now, the time to write the JSInterface class and specialClick() method.
Create a class named JSInterface and put the code ...
public class JSInterface
{
public void specialClick(String param1, String param2)
{
Log.d("CZ","param1..." + param1 + " , param2..." + param2);
handler.post(new Runnable()
{
public void run()
{
}
});
}
}
In such way , whatever parameters you have passed from HTML onClick method , you will get it in Android code. So, connection between WebView javascript and Android native code is done.
For, further clarification and doubts , you may contact me .... surojit.pakira@collectivezen.com
Hi Surojit,
ReplyDeletethank you for your example.
Could you please post here the working code?
Then (not related to your example) did you happen to try to do some string manipulation in a JavaScript? (on Android)
I couldn't split a string in JS, nor even look for a substring inside the JS method. I was thinking that you have experience with that as well
Thanks a lot,
Cristian
Hello Cristian,
ReplyDeleteThanks a lot for the comment !!!
Yeah , I will post a working code over here soon.
For your last question , could you please elaborate the question ? If you do so, then I might help you in that matter !!
Regards,
Surojit
Hi there, thanks for the code but I believe js method should not have window in the end. So it should be
ReplyDeletejsinterface.method()
not
window.jsinterface_method()
Thanks
(reference http://developer.android.com/guide/webapps/webview.html#BindingJavaScript)
Nice blog post!!! Very informative and appreciable . keep posting
ReplyDeleteBest sap institute in India ,SAP online training & sap training courses
SAP ABAP on HANA Online Training
sap hana online training
sap S/4 HANA training online
SAP UI5 Online Training in India
Learn SAP online
sap fiori training online
sap fico training online
Thanks
Perde Modelleri
ReplyDeletesms onay
Turkcell Mobil Ödeme Bozdurma
nftnasilalinir
ankara evden eve nakliyat
trafik sigortası
dedektör
web sitesi kurma
aşk kitapları
Smm panel
ReplyDeletesmm panel
İŞ İLANLARI
İNSTAGRAM TAKİPÇİ SATIN AL
hirdavatciburada.com
Https://www.beyazesyateknikservisi.com.tr
servis
tiktok jeton hilesi
tuzla toshiba klima servisi
ReplyDeleteçekmeköy lg klima servisi
maltepe mitsubishi klima servisi
kadıköy mitsubishi klima servisi
pendik arçelik klima servisi
tuzla samsung klima servisi
çekmeköy vestel klima servisi
ataşehir vestel klima servisi
çekmeköy bosch klima servisi
Success Write content success. Thanks.
ReplyDeletecanlı poker siteleri
deneme bonusu
betmatik
kıbrıs bahis siteleri
kralbet
betturkey
canlı slot siteleri
Good content. You write beautiful things.
ReplyDeletetaksi
mrbahis
mrbahis
sportsbet
sportsbet
hacklink
korsan taksi
hacklink
vbet
yurtdışı kargo
ReplyDeleteresimli magnet
instagram takipçi satın al
yurtdışı kargo
sms onay
dijital kartvizit
dijital kartvizit
https://nobetci-eczane.org/
XYİ
gümüşhane
ReplyDeletebilecik
erzincan
nevşehir
niğde
DF804G
resimli magnet
ReplyDeleteresimli magnet
çerkezköy çatı ustası
silivri çatı ustası
dijital kartvizit
7RQN
دهان ضد الصدأ
ReplyDeleteبرايمر دهان الحديد ضد الصدأ
شركة كشف تسربات المياه بالجبيل PjvolZt1TN
ReplyDelete