I use webView1 + local html file like a custom navigation bar with some links.
When the link is clicked, the asked html file must load in webView2.
Is this possible?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // Fixed Portrait orientation
requestWindowFeature(Window.FEATURE_NO_TITLE); // No app title bar
setContentView(R.layout.activity_main);
myNavbarView = (WebView)findViewById(R.id.navbarView);
myNavbarView.getSettings().setJavaScriptEnabled(true);
myNavbarView.setVerticalScrollBarEnabled(false);
myNavbarView.setHorizontalScrollBarEnabled(false);
myNavbarView.setWebViewClient(new WebViewClient());
myNavbarView.loadUrl("file:///android_asset/HTML/navbar.html");
myWebView = (WebView)findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
//hack to load twitter
myWebView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
myWebView.setVerticalScrollBarEnabled(false);
myWebView.loadUrl("file:///android_asset/HTML/index.html");
Not sure why you would use two webViews like this.
However, one solution would be to add a Javascript interface bridge to your navWebView and let it pass the URL back to Java then the Java can pass it to the second webView via loadUrl .
Info on JSInterface: http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29
Simplefied work out from this example: http://blog.objectgraph.com/index.php/2012/03/16/android-development-javascript-bridge-example-fully-explained/
JavaScriptHandler.java
public class JavaScriptHandler {
MainActivity parentActivity;
public JavaScriptHandler(MainActivity activity) {
parentActivity = activity;
}
public void setResultUrl(String url){
this.parentActivity.javascriptCallUrl(url);
}
}
MainActivity.java
// add line:
myNavbarView.addJavascriptInterface(new JavaScriptHandler(this), "MyHandler");
// Verwerk calls van navbar.html via JavaScriptHandler.java
public void javascriptCallUrl(final String url){
Log.v(TAG, "MainActivity JSHandler used: " + url);
// I need to run set operation of UI on the main thread.
// therefore, the above parameter "url" must be final
runOnUiThread(new Runnable() {
public void run() {
myWebView.loadUrl(url);
}
});
}
navbar.html
<a href="#" onclick="window.MyHandler.setResultUrl('file:///android_asset/HTML/index.html')">...</a>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.