Webview URL redirects/changes when launch tablet
-
30-06-2021 - |
Вопрос
I'm using webview in my application and giving hardcoded URL using the code. All things are working perfectly fine, but one thing came across when I was testing it on 10.1" tablet. The URL which I have provided works fine with the phone but on tablet it redirects itself to the desktop version of the URL.
Any idea how to prevent that from happening. I'm using this URL: http://search.yahoo.com/mobile/s?submit=oneSearch&.intl=us&.lang=en&.tsrc=yahoo&.sep=fp&x=0&y=0&p=bike
I have specifically mentioned mobile in the URL, but still it is redirecting. Any idea..??
Решение 3
@Eldhose M Babu and @ankitmakwana: I have used that, but I''m not sure what is missing in that. I have attached the code, please have a look and guide me:
mWebview.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
mWebview.setVisibility(View.GONE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.setVisibility(View.VISIBLE);
mWebview.requestFocus();
}
public void onLoadResource(WebView view, String url) {
mWebview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('header')[0].style.display = 'none'; "
+ "})()");
mWebview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('footer')[0].style.display = 'none'; "
+ "})()");
mWebview.loadUrl("javascript:(function() { "
+ "document.getElementsByTagName('section').search_again.style.display = 'none'; "
+ "})()");
}
});
mWebview.loadUrl("http://search.yahoo.com/mobile/s?submit=oneSearch&.intl=us&.lang=en&.tsrc=yahoo&.sep=fp&x=0&y=0&p=bike");
setContentView(mWebview);
Другие советы
need to use webview client
WebView web = (WebView) findViewById(R.id.webView1);
web.loadUrl(myurl.trim());
web.setWebViewClient(new HelloWebViewClient());
private class HelloWebViewClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
Log.i(General.TAG,Tag+"Page Loading is Started...");
web.setVisibility(View.GONE);
pbr.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
Log.i(General.TAG,Tag+"Page Loading is Finished");
web.setVisibility(View.VISIBLE);
pbr.setVisibility(View.GONE);
web.requestFocus();
}
}
In order to handle the redirecting you need to use WebViewClient
Then override the shouldOverrideUrlLoading method.
"Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url."