I need to take url after loading a web page in my web view to check condition and if I use onPageFinished() method, then where to use.
Just set a webview client and override onpagefinished as shown below
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webview);
webView.loadUrl("http://www.google.com");
webView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getApplicationContext(), url,Toast.LENGTH_LONG).show();
}
});
}
}
inside onpagefinished you have the url
Related
I have a class which extends WebViewFragment and what I want to achieve is to be able to load other URL from another class into this WebViewFragment class. Here is how my code looks like:
public class FragWeber extends WebViewFragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
WebView webView = getWebView();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.google.com");
}
}
Any idea how to access the WebView to load other URL?
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String url1 = url; // can add your URL address
view.loadUrl(url);
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
guys! I have problem to load html page with android-webview. I need to load the url with my webview but not with the mobile system broswer or other broswer, so I have to apply the method setWebViewClient() to my webview but not WebChromeClient(). However, there's load nothing but blank page when applied the setWebViewClient(), and works fine with the later method. I don't know where's problem, here is the code:
.xml :
<WebView android:id="#+id/webview"
android:layout_marginTop="50dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
.java :
WebView webView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT >= 19) {
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
private String loadUrl = "http://www.baidu.com";
webView.loadUrl(loadUrl);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
view.loadUrl(url);
super.onLoadResource(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.loadUrl(url);
super.onPageStarted(view, url, favicon);
}
});
/*
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
Log.v(Log_Tag, String.valueOf(newProgress));
}
});
*/
Just put this code in your activity
private String loadUrl = "https://www.google.com";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mWebView = (WebView) findViewById(R.id.webView);
WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
WebViewClient mWebViewClient = new WebViewClient();
mWebView.setWebViewClient(mWebViewClient);
mWebView.loadUrl(loadUrl);
}
You shouldn't override methods in WebViewClient class if you don't want to change their behavior or add some functionality. And don't forget to add permission Internet to your manifest file. WebView will not work without it.
Thanks #Mike M. again. The method shouldOverrideUrlLoading() should return false if you want to loading the url with your webView but not with the mobile system default browser or other Third-Party browsers. And, if you want to deal some javascript actions of the webpage with your webView, you're suggested to apply the WebChromClient to your webView.
Here is the good example:
WebView webView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//load the page with cache
if (Build.VERSION.SDK_INT >= 19) {
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//return true load with system-default-browser or other browsers, false with your webView
return false;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
Log.v(Log_Tag, String.valueOf(newProgress));
//put your code here if your want to show the progress with progressbar
}
});
private String loadUrl = "http://www.baidu.com";
webView.loadUrl(loadUrl);
public class MainActivity extends AppCompatActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.w3schools.com/");
emphasized text
I have a WebView trying to load a WebPage like this:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
wv=(WebView)findViewById(R.id.webview);
WebSettings ws=wv.getSettings();
ws.setJavaScriptEnabled(true);
wv.loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
}
I have added the internet permission to the manifest
<uses-permission android:name="android.permission.INTERNET"/>
However the WebView is blank,it neither throws an error nor loads the web-page.How do I load webpages like this,I have only tried loading local html files before and I would like to know if I have to do something different.
Try like this way:
mWebView.loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
mWebView.setWebViewClient(new HelloWebViewClient());
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
Use this code .shouldOverrideUrlLoading() not use compulsary.use this code and please reply me this code work or not
Try below code.
public class Main extends Activity {
private WebView mWebview ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebview = (WebView)findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://pro39.blutechnologies.com/crimes.aspx");
setContentView(mWebview );
}
}
I am working on a android project. In that project i have to load a URL in WebView.
But I am unable to Load the particular URL (Any other URL is loading perfectly). I have also added INTERNET permission. Though the URL open from Android browser .
public class MainActivity extends Activity {
private WebView webView;
private ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView)findViewById(R.id.webview1);
progDailog = ProgressDialog.show(this, "Loading","Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
// webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progDailog.show();
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
progDailog.dismiss();
}
});
webView.loadUrl("https://XXXXX.com/");
}
}
This site might contain insecure content.Check your proxy settings for the WIFI network connection and make sure they are correct.
I have an initialized WebView with the following code:
_webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
while (view.zoomOut());
}
_webView.loadURL("path/to/image.png");
But this does not work. The WebView zooms out while loading. But when it is finished, it will zoom in.
Is there any other way to zoom out completely after a page (I use the WebView to display an image) is loaded?
Try this piece of code:
public class WebViewSampleActivity extends Activity
{
WebView wb;
private class HelloWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wb=(WebView)findViewById(R.id.webView1);
wb.getSettings().setJavaScriptEnabled(true);
wb.getSettings().setLoadWithOverviewMode(true);
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setBuiltInZoomControls(true);
wb.getSettings().setPluginState(WebSettings.PluginState.ON);
wb.getSettings().setPluginsEnabled(true);
wb.setWebViewClient(new HelloWebViewClient());
wb.loadUrl("http://www.foofoo.com");
}
}
Hope this helps.
Don't do anything in onPageFinished() instead use webview.getSettings().setDefaultZoom(WebviewSetting.ZoomDensity.FAR);
Hope this helps
These are the two most important commands when loading images:
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);