I was wondering if there exits another effective way to get the URL that will load up on the WebView. My current code gives the current Url and not the one that will be loaded.
For example if my WebView load this: http://stackoverflow.com
After loading If I click on Questions, I would not get this url http://stackoverflow.com/questions, for some reason I would get http://stackoverflow.com
So my question is how would you get the url that will be loading on a WebView?
This is my code, please help!
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
// Declaring
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing
browser = (WebView) findViewById(R.id.webView1);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);
browser.getSettings().setBuiltInZoomControls(true);
// Loading
browser.loadUrl("http://stackoverflow.com");
browser.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource(WebView view, String url) {
//here I get the Url, but its not accurate. Sometimes it works, sometiems it doesn't
Toast.makeText(getApplicationContext(), browser.getUrl(),
Toast.LENGTH_SHORT).show();
}
});
}
}
Try to get url from this function :
EDIT:
browser.loadUrl("http://stackoverflow.com");
browser.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show();
Log.v("TEST", url);
if(url.equals("http://stackoverflow.com/questions")){
Toast.makeText(getApplicationContext(), "SKIP", Toast.LENGTH_SHORT).show();
}
else{
view.loadUrl(url);
}
return true;
}
});
Reference :
shouldOverrideUrlLoading
I know its very late but,for other users if it helps it would be my pleasure.
In ur WebViewClient u can use :
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String webUrl=view.getUrl();
return true;
}
}
Related
I know this has been asked multiple times but I went through all the answers and I just cant get it to work. Im trying to make a simple webview app that handles external links in native browser.
package com.example.app;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private WebView webview;
private long backPressedTime;
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
if (backPressedTime + 2000 > System.currentTimeMillis()) {
super.onBackPressed();
return;
} else {
Toast.makeText(getBaseContext(), "Press back again to exit", Toast.LENGTH_SHORT).show();
}
backPressedTime = System.currentTimeMillis();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webView);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
webview.loadUrl("https://www.myurl.com");
}
}
Any help would be greatly appreciated. Thank you
You can override URL loading with following code. If you want to handle URL loading yourself return true. If the URL should be opend in the WebView, return false.
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Insert logic here
}
}
If you just want to open external links you can use following code. In case the opened URL starts with your domain's base URL, false is returned and the URL is opened in the WebView. Otherwise the ACTION_VIEW intent is used to open the URL in the browser and true is returned.
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Abort if no URL
if (url == null || !(url.startsWith("http://") ||
url.startsWith("https://"))) {
return false;
}
// Abort if internal URL
if (url.startsWith("http://www.myurl.com") ||
url.startsWith("https://www.myurl.com")) {
return false;
}
// Open external URL in browser
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
});
Notice: You have to handle "http://..." and "https://...". Because the method might get called for both.
I ma newbie to android and making a demo on webview,I have a test url which is working fine with my laptop browser and i have set it in my webview,but it is not loading and it says "Webpage not available",Please help me to figure it out,need help,
code
package namo.jims.com.webviewapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview= (WebView)findViewById(R.id.webview);
webview.setWebViewClient(new MyBrowser());
webview.loadUrl("http://rchitecture.in/app1/");
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setJavaScriptEnabled(false);
webview.getSettings().setAllowFileAccess(true);
webview.getSettings().setAllowContentAccess(true);
webview.setScrollbarFadingEnabled(false);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You need to add the Internet Permission
<uses-permission android:name="android.permission.INTERNET"/>
You need to follow my answer here...
Snapchat Url load issue
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
//ws.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Log.i(TAG, "Processing webview url click..."+url);
// view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.e(TAG, "Finished loading URL: " + url);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
});
webview.loadUrl("http://rchitecture.in/app1/");
I am creating a simple app Android webview, but also following the guides, I can not implement a progress bar to my code.
how can I implement a loading bar that will disappear after the page loads?
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyBrowser());
view.loadUrl("http://www.abcdefcsadfg.org");
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
You can initialize a Progressbar in onCreate, like below and set its initial visibility to View.GONE.
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setVisibility(View.GONE);
And your MyBrowser should look like this
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progress.setVisibility(View.GONE);
super.onPageFinished(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progress.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
}
UPDATE:
Check this link
You should declare progress bar in xml file and refer to it in the main activity so that it appears in your application.
Set its progress in MyBrowser.
Refer this link, it will help you.
http://javatechig.com/android/progressbar-while-loading-webview
If I use a button it works, but it redirects to a default browser.
I want to use the webview because i want to display the website in my webview and avoid the address bar to display.
Here is my code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
WebView webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=http://178.239.16.28/fzs/sites/default/files/dokumenti-vijesti/sample.pdf");
webview.setWebViewClient (new WebViewClient());
}
Try below code sequence and let me know if any issues. Don't forget to add permission to your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Change to below :
WebView webview = (WebView) findViewById(R.id.webView1);
webview.setWebViewClient (new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=http://178.239.16.28/fzs/sites/default/files/dokumenti-vijesti/sample.pdf");
And use below snippts
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
Your url is redirecting.
You'll have to override shouldOverrideUrlLoading
Give the host application a chance to handle the key event synchronously. e.g. menu shortcut key events need to be filtered this
way. If return true, WebView will not handle the key event. If return
false, WebView will always handle the key event, so none of the super
in the view chain will see the key event. The default behavior returns
false.
Parameters
view The WebView that is initiating the callback.
event The
key event.
Returns True if the host application wants to handle the
key event itself, otherwise return false
Use this lines ..
package org.example.webviewdemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewDemo extends Activity {
private WebView webView;
Activity activity ;
private ProgressDialog progDailog;
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
progDailog.setCancelable(false);
webView = (WebView) findViewById(R.id.webview_compontent);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
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("http://docs.google.com/gview?embedded=true&url=http://178.239.16.28/fzs/sites/default/files/dokumenti-vijesti/sample.pdf");
}
}
It will show loader until loading the file. then loader get hide once file have loaded using google docs. hope this will help you.
I developed a small android webview app to access an internal (local network) PHP based site.
Here is my code:
package com.CheckInventory;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
#SuppressWarnings("unused")
public class CheckInventoryActivity extends Activity {
WebView webview;
String username;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebView webView = (WebView) findViewById(R.id.webview);
webView.setBackgroundColor(0);
webView.setBackgroundResource(R.drawable.myimage);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
WebSettings webSettings = webview.getSettings();
webSettings.setLoadWithOverviewMode(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
webSettings.setDomStorageEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://192.168.0.124/android");
webview.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()){
//webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
The site has authentication, but I would like to add some authentication between the app and the site (How do I do this? pass a parameter maybe when the url is invoked), secondly and more importantly, where specifically do I put the onReceivedError so the user never sees the url or the page down if they walk away from the building or loose connection.
public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
Toast.makeText(Webform.this, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
}
I saw this explanation in Detecting Webview Error and Show Message but I do not know where to implement it.
Thank you in advance
You need to create class which extends WebViewClient and implements method OnReceivedError
like this
class myWebClient extends WebViewClient {
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(Webform.this, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
and then you need to set new WebViewClient to your WebView
web = (WebView) findViewById(R.id.webview);
web.setWebViewClient(new myWebClient());
Hope this helps
You can add onReceivedError in Your HelloWebViewClient class and handle what you want to handle when you get error.
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show();
}
}