I need a page which show web contents in a fixed width and height.
I put a webview inside a xml file with a height of 200px,but while loading that page,it directly redirect to default browser view.
While press the back button in the phone,it redirect to the original page,then the webview shows nothing.
in my xml file
<WebView android:layout_width="fill_parent" android:layout_height="200px" android:id="#+id/wvbrowser" />
in code
WebView wvbrowser;
wvbrowser=(WebView)findViewById(R.id.wvbrowser);
wvbrowser.loadUrl("http://www.orkut.com");
So tell me where I am Wrong? and how to show WebView in FIXED size?
Try this...
create a separate class.
public class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Then in your activity use this.
WebView wvbrowser;
wvbrowser=(WebView)findViewById(R.id.wvbrowser);
wvbrowser.setWebViewClient(new HelloWebViewClient());
wvbrowser.getSettings().setJavaScriptEnabled(true);
wvbrowser.loadUrl("http://www.orkut.com");
When the user clicks a link from a web page in your WebView,the default Android Browser handles the Intent to view a web page, because your Activity isn't technically enabled to do so. You need to override the WebViewClient class and enable your webview Activity to handle its own URL requests.
setWebViewClient(new HelloWebViewClient());
The above line creates a WebViewClient that will load any URL selected from this WebView into the same WebView.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
The shouldOverrideUrlLoading(WebView, String) method is passed the current WebView and the URL requested, so all it needs to do is load the URL in the given view. Returning true says that the method has handled the URL and the event should not propagate.
Related
I have a webview that looks like this
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("https://myurl.com/");
webview.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Toast.makeText(LoginActivityWebView.this, url, Toast.LENGTH_SHORT).show();
return false;
}
}
When i change my URL to google everything works great, i can navigate to different sites and everything works.
When I put in my URl it is supposed to link to myurl2, but when I click it the toast prints myurl instead and the page juts reloads (not helpful!).
Any idea as to how this could happen and where shouldOverrideUrlLoading gets its url from?
If i load myurl in a browser and the user clicks a button then it works correctly.
If i load myurl in a webview, but open a browser on links pressed, then when the link is pressed it opens myurl on in a broswer and if you click the button again it correctly opens myurl2 (super weird).
Any ideas?
If the URL is opening in Chrome properly, you should consider adding, webView.getSettings().setJavaScriptEnabled(true) to your code and try again.
You can get the loaded URL from
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
I have a webview on my MainActivity .. This loaded with a url set by a onclick button handling.
When the webview is loaded with a certain webpage.
I want to clone or duplicate (or even move) it to my secondary display/screen.
For the secondary display i have a seperate Presentation class:
private class BrowserPresentation extends Presentation {
BrowserPresentation(Context ctxt, Display display) { super(ctxt, display); }
WebView webView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView2=new WebView(webView.getContext()); //get webView class of mainactivity
webView2.setWebViewClient(new WebViewClient());
webView2.setInitialScale(100);
setContentView(webView2);
}
}
However this is not working, is it even possible? Am I taking the wrong route? Can't find much on internet about secondary screen handling.
Update:
I don't want to load same page, need to get current page loaded on MainActivity: Want to login on MainActivity then after the login duplicate/move it to second screen(readonly).
As per Android dev page :
Handling Page Navigation
When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView. You can then allow the user to navigate backward and forward through their web page history that's maintained by your WebView.
To open links clicked by the user, simply provide a WebViewClient for your WebView, using setWebViewClient(). For example:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
That's it. Now all links the user clicks load in your WebView.
If you want more control over where a clicked link load, create your own WebViewClient that overrides the shouldOverrideUrlLoading() method. For example:
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
Then create an instance of this new WebViewClient for the WebView:
[1]: https://developer.android.com/guide/webapps/webview.html
I'm using WebView in android; I want to show ZoomControls not for all pages but only for my specific webpage. What should I do? Please Help!
Assuming that it's an activity and you are receiving the url from the intent
String url = getIntent().getStringExtra("url");
WebView webview = (WebView) findViewById(R.id.webview_id_here)
webView.getSettings().setBuiltInZoomControls(url.equals("your_url_here"));
If you want this to happen every time a url changes, use a custom webview client.
webView.setWebViewClient(new CustomWebViewClient());
wherever the webView is initialized. CustomWebViewClient :
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
view.getSettings().setBuiltInZoomControls(url.equals("your_url_here"));
return false;
}
}
When a user clicks on a link in the webview. I want to show a dialog asking whether the user wants to view it in default browser, if he opts YES then he should be taken to default browser otherwise it shouldn't load the link at all.
But, the issue I am facing is, I could able to show the dialog inside run() of WebViewClient's overridden method shouldOverrideUrlLoading(). When the user opts YES I am doing startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(url))); to show it in default browser. But, irrespective of the user selects for YES/NO it is showing the link in webview which I want to avoid. Any suggestions appreciated...TIA
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
http://developer.android.com/guide/webapps/webview.html
I resolved the issue, by just reloading the same URL inside shouldOverrideUrlLoading() method. Thanks for your suggestions
I have written a simple helloworld app with a WebView which has a link to CNN on a simple.html page in my asset folder.
cnn.com
How can I capture the click on this on my Activity, stop the WebView from navigating, and then inform the Activity that "http://CNN.com" was clicked?
Then you have to set a WebViewClient to your WebView and override shouldOverrideUrlLoading and onLoadResource methods. Let me give you a simple example:
WebView yourWebView; // initialize it as always...
// this is the funny part:
yourWebView.setWebViewClient(yourWebClient);
// somewhere on your code...
WebViewClient yourWebClient = new WebViewClient(){
// you tell the webclient you want to catch when a url is about to load
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
return true;
}
// here you execute an action when the URL you want is about to load
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("http://cnn.com") ){
// do whatever you want
}
}
}