I cannot load page using link that is passed from a previous Activity.
The code is as follows
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_webpage);
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
/*Bundle b = getIntent().getExtras();
String url = b.getString(DeviceDetails.URL_KEY);*/
String url = getIntent().getStringExtra(DeviceDetails.URL_KEY);
//String url = "http://google.pl/nexus/4";
webView.setWebViewClient(new MyWebViewClient());
webView.loadUrl(url);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
So, when I use String url="http://google.pl/nexus/4" all seems to be fine. And I'm totally sure that my activity gets the url from getIntent because I debugged it.
UPD1:
String inputUrl = detUrlEditText.getText().toString();
Intent intent = new Intent(DeviceDetails.this, ShowWebPageActivity.class);
Bundle extras = new Bundle();
extras.putString(URL_KEY, inputUrl);
intent.putExtras(extras);
startActivity(intent);
Previous Activity. It is guaranteed to pass url because I debugged it. And toast also shows passed url in ShowWebPageActivity.
Don't call view.loadUrl(url) inside the method shouldOverrideUrlLoading().
I've seen examples elsewhere that do this and I don't understand why, this shouldn't be necessary.
shouldOverrideUrlLoading() should return true when you are handing the URL and the WebView should not load it, and return false when the WebView should proceed with loading the URL.
You could call view.loadUrl() if you decide that the WebView should open a different page than the URL parameter, for example.
Here's an example of a WebViewClient subclass:
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (handleWithSystemBrowser(url)) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
return true; // tells WebView that we specifically handled the URL, so don't load it
}
return false; // go ahead and load it in the WebView
}
private boolean handleWithSystemBrowser(String url) {
// put your code here to check the URL
// return true if you want the device browser to display the URL
// return false if you want the WebView to load the URL
.
.
.
}
}
Related
I want to do an app that is mainly base on webview. MainActivity is to load www.example.com/products.php.
www.example.com/products.php - display all products*
www.example.com/products.php?id=123 - display individual product base on product ID
www.example.com/cart.php - when clickcing on "Add to cart" on product.php?id=123, it will not open a new Activity but remain in the same activity.
MainActivity.java
private WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webView);
WebSettings wss = wv.getSettings();
wss.setJavaScriptEnabled(true);
wv.loadUrl("http://www.example.com/product.php");
wv.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/cart.php")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}else{
// 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;
}
}
}
#Override
public void onBackPressed() {
if(wv.canGoBack()){
wv.goBack();
}else{
super.onBackPressed();
}
}
And also for "shouldOverrideUrlLoading " , it shows depreciated... so what's the new name for this?
I have a webview and I am calling data in that wv from a webservice, and in that whole description in a webview, there is a link in the last. so, my problem is that i want to open a new activity onclick of that link only neither onclick of webview nor ontouch of webview
You need to provide an implementation for shouldOverrideUrlLoading. You'll have to setup a WebViewClient for your webview and inside of this method, you'll need to have some logic that recognized that link and then open the new Activity. Something like:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.myWebView);
wv.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(isURLMatching(url)) {
openNextActivity();
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
}
protected boolean isURLMatching(String url) {
// some logic to match the URL would be safe to have here
return true;
}
protected void openNextActivity() {
Intent intent = new Intent(this, MyNextActivity.class);
startActivity(intent);
}
I am trying to open the url in webview, but it is opening the url in external browser.
My code is
public class MainActivity extends Activity
{
private WebView webView;
String URL = "https://login.salesforce.com/services/oauth2/authorize?response_type=token&display=touch&client_id=3MVG9Y6d_Btp4xp7w.6oGLerlRztTTUKMEL8QWHvuB5miUgdJzQ0HgnMB7dhm1mTiluRv4ud9cZYeFcAz7hXP&redirect_uri=https%3A%2F%2Flogin.salesforce.com%2Fservices%2Foauth2%2Fsuccess";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl(URL);
//webView.loadUrl("https://www.google.co.in/");
webView.setWebViewClient(new MyWebViewClient());
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(url)) {
// This is your web site, so do not override; let the WebView to 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;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
// this will ignore the Ssl error and will go forward to your site
handler.proceed();
}
}
}
If the url is https:www.google.com then the url is opening in webview only. I have googled hard to find the solution but I am unable to find it. How to resolve the issue?
The issue is in your
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals(url)) {
// This is your web site, so do not override; let the WebView to 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;
}
its trying to load the URL as a different intent, hence your default browser kicks in.
what do you really want to check with
Uri.parse(url).getHost().equals(url)
try putting logs for the check you will figure out what wrong.
Here's my code:
public class MainActivity extends Activity {
#SuppressLint("SetJavaScriptEnabled") #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webview);
mywebview.loadUrl("http://www.shufflemylife.com/shuffle");
WebSettings webSettings = mywebview.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebview.setWebViewClient(new WebViewClient());
}
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("/shuffle")){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
return true;
}
}
}
Basically, I want any url containing '/shuffle' to load inside WebView, and anything else to be opened in the external browser. Is it doable? How close am I do accomplishing this?
Thanks for any help!
To clarify, this is how you should write your shouldOverrideUrlLoading method:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("/shuffle")) {
mWebView.loadUrl(url);
return false;
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
}
Is it doable?
Yes.
How close am I do accomplishing this?
Close, but backwards. The default behavior of a WebView is to display links in the external browser. Hence, if url.contains("/shuffle"), you want to call loadUrl() on your WebView to keep the link internal, and return true in that case. If this is a URL you want handled normally, return false.
I've added a webview activity and would like to control which folder any downloaded that my end users will download via my app. The files will be pdf files and I would like them to go into a specific folder that my app uses rather than the default download folder.
public class myweb extends Activity {
private WebView myWebView;
private String LOG_TAG = "AndroidWebViewActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.knitweb);
myWebView = (WebView) findViewById(R.id.webView1);
//enable Javascript
myWebView.getSettings().setJavaScriptEnabled(true);
//loads the WebView completely zoomed out
myWebView.getSettings().setLoadWithOverviewMode(true);
//true makes the Webview have a normal viewport such as a normal desktop browser
//when false the webview will have a viewport constrained to it's own dimensions
myWebView.getSettings().setUseWideViewPort(true);
//override the web client to open all links in the same webview
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.setWebChromeClient(new MyWebChromeClient());
//Injects the supplied Java object into this WebView. The object is injected into the
//JavaScript context of the main frame, using the supplied name. This allows the
//Java object's public methods to be accessed from JavaScript.
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
//load the home page URL
//myWebView.loadUrl("http://demo.mysamplecode.com/Servlets_JSP/pages/androidWebView.jsp");
myWebView.loadUrl("http://www.patonsyarns.com/pattern.php?PID=4521&cps=21191");
myWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("demo.mysamplecode.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
private class MyWebChromeClient extends WebChromeClient {
//display alert message in Web View
#Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d(LOG_TAG, message);
new AlertDialog.Builder(view.getContext())
.setMessage(message).setCancelable(true).show();
result.confirm();
return true;
}
}
public class JavaScriptInterface {
Context mContext;
// Instantiate the interface and set the context
JavaScriptInterface(Context c) {
mContext = c;
}
//using Javascript to call the finish activity
public void closeMyActivity() {
finish();
}
}
//Web view has record of all pages visited so you can go back and forth
//just override the back button to go back in history if there is page
//available for display
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
I do not know in advance which files the users will be downloading other than that they will be pdf files.
Any help would be great
Thanks
I’m affraid the only solution is to use DownloadListener to catch the event and download the file manually.
Download a file example: http://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/
DownloadListener example: How do I use DownloadListener?