java - Call JS function in webview from Static fragment -


i trying have tabular activity automatically setup me when creaked project selecting navigation type of "fixed tabs + swype". in each tab load webview changes url based on tab. in 1 tab though want call js function lives on page webview going to. tried sorts of things no luck, js function works reason not being called.

the url in code below not url i'm using, reference here js function

function callfromactivity(msg){     document.getelementbyid("mytext").innerhtml = msg; }  public class mainactivity extends fragmentactivity implements actionbar.tablistener {  /**  * {@link android.support.v4.view.pageradapter} provide  * fragments each of sections. use  * {@link android.support.v4.app.fragmentpageradapter} derivative,  * keep every loaded fragment in memory. if becomes memory  * intensive, may best switch  * {@link android.support.v4.app.fragmentstatepageradapter}.  */ sectionspageradapter msectionspageradapter;  /**  * {@link viewpager} host section contents.  */ viewpager mviewpager;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      // set action bar.     final actionbar actionbar = getactionbar();     actionbar.setnavigationmode(actionbar.navigation_mode_tabs);      // create adapter return fragment each of 3     // primary sections of app.     msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager());      // set viewpager sections adapter.     mviewpager = (viewpager) findviewbyid(r.id.pager);     mviewpager.setadapter(msectionspageradapter);      // when swiping between different sections, select corresponding     // tab. can use actionbar.tab#select() if have     // reference tab.     mviewpager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() {         @override         public void onpageselected(int position) {             actionbar.setselectednavigationitem(position);         }     });      // each of sections in app, add tab action bar.     (int = 0; < msectionspageradapter.getcount(); i++) {         // create tab text corresponding page title defined         // adapter. specify activity object, implements         // tablistener interface, callback (listener) when         // tab selected.         actionbar.addtab(                 actionbar.newtab()                         .settext(msectionspageradapter.getpagetitle(i))                         .settablistener(this));     } }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) {     // when given tab selected, switch corresponding page in     // viewpager.     mviewpager.setcurrentitem(tab.getposition()); }  @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { }  @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { }  /**  * {@link fragmentpageradapter} returns fragment corresponding  * 1 of sections/tabs/pages.  */ public class sectionspageradapter extends fragmentpageradapter {      public sectionspageradapter(fragmentmanager fm) {         super(fm);     }      @override     public fragment getitem(int position) {         // getitem called instantiate fragment given page.         // return dummysectionfragment (defined static inner class         // below) page number lone argument.         fragment fragment = new dummysectionfragment();         bundle args = new bundle();         args.putint(dummysectionfragment.arg_section_number, position + 1);         fragment.setarguments(args);         return fragment;     }      @override     public int getcount() {         // show 5 total pages.         return 5;     }      @override     public charsequence getpagetitle(int position) {         locale l = locale.getdefault();         switch (position) {             case 0:                 return getstring(r.string.title_section1).touppercase(l);             case 1:                 return getstring(r.string.title_section2).touppercase(l);             case 2:                 return getstring(r.string.title_section3).touppercase(l);             case 3:                 return getstring(r.string.title_section4).touppercase(l);             case 4:                 return getstring(r.string.title_section5).touppercase(l);         }         return null;     } }  /**  * dummy fragment representing section of app,  * displays dummy text.  */ public static class dummysectionfragment extends fragment {     /**      * fragment argument representing section number      * fragment.      */     public static final string arg_section_number = "section_number";     public dummysectionfragment() {     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {          view rootview = inflater.inflate(r.layout.fragment_main_dummy, container, false);         view htmlview =inflater.inflate(r.layout.home_wvtc,container,false);         webview wc = (webview) htmlview.findviewbyid(r.id.webview1);         textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label);         // dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number)));         if(getarguments().getint(arg_section_number) == 1 )         {              wc.getsettings().setjavascriptenabled(true);             wc.loadurl("http://www.example.com/android/index.html");             wc.loadurl("javascript:callfromactivity('some msg')");              wc.setwebviewclient(new webviewclient(){                 public boolean shouldoverrideurlloading(webview view, string url) {                     return false;                 }             });              return htmlview;         }         if(getarguments().getint(arg_section_number) == 2 )         {              wc.getsettings().setjavascriptenabled(true);             wc.loadurl("http://www.example.com/android/request.html");              wc.setwebviewclient(new webviewclient(){                 public boolean shouldoverrideurlloading(webview view, string url) {                     if (url != null ) {                          return false;                     } else {                         return false;                     }                 }             });              return htmlview;         }         if(getarguments().getint(arg_section_number) == 3 )         {              wc.getsettings().setjavascriptenabled(true);             wc.loadurl("http://www.example.com/android/recent.html");             wc.setwebviewclient(new webviewclient(){                 public boolean shouldoverrideurlloading(webview view, string url) {                     if (url != null ) {                          return false;                     } else {                         return false;                     }                 }             });             return htmlview;         }         if(getarguments().getint(arg_section_number) == 4 )         {              wc.getsettings().setjavascriptenabled(true);             wc.loadurl("http://www.example.com/android/queue.html");             wc.setwebviewclient(new webviewclient(){                 public boolean shouldoverrideurlloading(webview view, string url) {                     if (url != null ) {                          return false;                     } else {                         return false;                     }                 }             });             return htmlview;         }         if(getarguments().getint(arg_section_number) == 5 )         {              wc.getsettings().setjavascriptenabled(true);             wc.loadurl("http://www.example.com/android/webcam.html");             wc.setwebviewclient(new webviewclient(){                 public boolean shouldoverrideurlloading(webview view, string url) {                     if (url != null ) {                          return false;                     } else {                         return false;                     }                 }             });             return htmlview;         }         else {             return rootview;         }     }   }  } 

you should wait document load complete before execute java script. can use webchromeclient watch load progress.

private class mywebchromeclient extends webchromeclient {      private webview mwebview;     public mywebchromeclient(webview webview)     {         mwebview = webview;     }      @override     public void onprogresschanged(webview view, int newprogress) {         super.onprogresschanged(view, newprogress);         if (newprogress == 100) {             mwebview.loadurl("javascript:callfromactivity('some msg')");         }     } } 

set mywebchromeclient webview

wc.getsettings().setjavascriptenabled(true); wc.loadurl("http://www.example.com/android/index.html"); wc.setwebchromeclient(new mywebchromeclient(wc)); 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -