I'm using a WebView in my application, I have a requirement to change the app title based on the page user is on. How can I do this in Android WebView?
I did this in iphone by following line
self.title = [webPage stringByEvaluatingJavaScriptFromString:@"document.title"]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Adds Progrss bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webview );
// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById( R.id.webview ); //This is the id you gave
//to the WebView in the main.xml
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true); //Zoom Control on web (You don't need this
//if ROM supports Multi-Touch
mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
// Load URL
Bundle b = getIntent().getExtras();
String url = b.getString("url");
Log.d(TAG, "url " + url);
mWebView.loadUrl(url);
// Sets the Chrome Client, and defines the onProgressChanged
// This makes the Progress bar be updated.
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress){
//Make the bar disappear after URL is loaded, and changes string to Loading...
myActivity.setTitle("Loading...");
myActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
myActivity.setTitle(R.string.app_name);
}
});
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
// return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onLoadResource(WebView view, String url) {
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
ImageView logoImageView = (ImageView)findViewById(R.id.logoimage);
logoImageView.setVisibility(View.GONE);
Log.d(TAG, "view.getTitle() " + view.getTitle());
myActivity.setTitle(view.getTitle());
}
});
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…