How can a link within a WebView load another layout using javascript?
Posted
by huffmaster
on Stack Overflow
See other posts from Stack Overflow
or by huffmaster
Published on 2010-06-10T19:10:09Z
Indexed on
2010/06/10
19:13 UTC
Read the original article
Hit count: 280
So I have 2 layout files (main.xml, featured.xml) and both each have a single WebView. When the application starts "main.xml" loads a html file into it's WebView. In this html file I have a link that calls javascript that runs code in the Activity that loaded the html. Once back in this Activity code though I try running setContentView(R.layout.featured) but it just bombs out on me. If I debug it just dies without any real error and if I run it the application just Force closes.
Am I going about this correctly or should I be doing something differently?
final private int MAIN = 1;
final private int FEATURED = 2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.wvMain);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setSupportZoom(false);
webview.addJavascriptInterface(new EHJavaScriptInterface(), "eh");
webview.loadUrl("file:///android_asset/default.html");
}
final class EHJavaScriptInterface {
EHJavaScriptInterface() {
}
public void loadLayout(final String lo) {
int i = Integer.parseInt(lo.trim());
switch (i) {
/****** THIS IS WHERE I'M BOMBING OUT *********/
case FEATURED: setContentView(R.layout.featured);break;
case MAIN: setContentView(R.layout.main);break;
}
}
}
© Stack Overflow or respective owner