Good day all,
i have made an app but i have hit a wall.
when you visit a website using a desktop browser like firefox or chrome and click on the download link it starts downloading the file, but when i open the same page in my android app that displays the page using webview and click on a download link nothing happens at all, it just sits there like it does not know what to do with that link. if you could help me out it would be a huge help and get me back on track again.
this is the download link i use for a pdf file on my site:
<img src="images/Art1.jpg" width="80" height="166" />Download
this is my main.xml file for my android app:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
please i will be most gratefull if anyone can help point me on the right path.
package com.jeffonlinelibrary;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPageLoader extends Activity
{
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
// handle download, here we use brower to download, also you can try other approach.
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
webView.loadUrl("http://jeffonlinelibrary.comuv.com/jeffOnlinelibraryApp");
}
}
Set DownloadListener to your WebView. And use Brower app or DownloadManager to download the file.
webview.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
// handle download, here we use brower to download, also you can try other approach.
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
Related
I'm just testing my application at the moment. However, it keeps on crashing on the loading screen. Once it loads it redirects to the login or users account if they have already logged in.
However, I keep getting these error messages in android studios console log when built the application.
W/cr_CrashFileManager: /data/user/0/chrisbeckett.********/cache/WebView/Crash Reports does not exist or is not a directory
// I've put the stars as I can't show the name of the application yet.
W/cr_AwContentsClient: Denied starting an intent without a user gesture, URI http://********.********.com/login
My current code:
package chrisbeckett.**********;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;// Instance of WebChromeClient for handling all chrome functions.
private volatile WebChromeClient mWebChromeClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
webSettings.setAppCacheEnabled(true);
webSettings.setAppCacheMaxSize(100 * 1000 * 1000);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.loadUrl("http://********.*********.com/loading");
}
/**
* Set the WebChromeClient.
* #param client An implementation of WebChromeClient.
*/
public void setWebChromeClient(WebChromeClient client) {
mWebChromeClient = client;
}
}
Further information:
My code works on all web browsers on a desktop when viewing it in a mobile mode in the inspector when testing before putting it in App format.
If you require any more information please let me know.
try below code this may work
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
You can use it for Android N
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
#TargetApi(Build.VERSION_CODES.N)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
android:usesCleartextTraffic="true" in application tag
add above line in your manifest.xml file.
I'm trying to load a web application in WebView but I've encounter some problem.
The web application that I was trying to load was a login page and I am going to login to it.
Here is where the problem start, I have no problem loading the 1st page but after I login and it should be showing the page after I login but all I can see was a blank page, I've checked on the login log that my account was logged in but there was nothing on the WebView.
Here's my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
webView.loadUrl(url);
}
I have tried it using WebChromeClient and it was able to load without problem but I want it to load inside WebView and not open another browser.
Anyone have any idea on why this is happening?
Try this sample,
MainActivity.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView webview;
private static final String TAG = "Main";
private ProgressDialog progressBar;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView)findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
progressBar = ProgressDialog.show(Main.this, "WebView Example", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
webview.loadUrl("http://www.google.com");
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="+#id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
This may helps you.
use this to load any url that you are willing to open into your web view xml
String url = "URL";
if (url != null){
webView.loadUrl(url);
}else{
String content = getIntent().getStringExtra(CONTENT_EXTRA);
webView.loadData(content, "text/html", null);
}
Please check your code with different URLs because there is no need to override shouldOverrideUrlLoading method to load the url again on login click.
I'm sorry for this as I am very Green at this Android Development, and I maybe beating a dead horse here-
A software tjat we use has a mobile version, but it's a mobile website, I am trying to build this into a standalone web-app using webview.
The app will get me to the login screen, but when I attempt to login the the pop-up showing its logging in, and in my webview, it is sticking there. It doesn't move past this point.
In the default browser it works fine.
Can you please assist me in what I need to do to get pass this in the most simple terms? ;-) Thank you!
package com.giantflyingsaucer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebPageLoader extends Activity
{
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://URL.USED.FOR/WEBAPP");
}
}
This was resolved by using
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
perhaps your login is trying to make use of some web plugin. Try adding one or both of these:
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setPluginsEnabled(true);
I am creating an app that allows college students to download their study material from within the app instead of the browser.
The home page has lots of subject names.
Each subject name leads to new webpage.
So, I have used WebViewClient.
But, at the final page when I click on the *.ppt or *.pdf files it opens junk.
I want these files to be downloaded within the app.
How do I implement DownloadListener
package jiit.app;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class sm extends Activity
{
WebView browser;
protected void onCreate(Bundle anyvar)
{
super.onCreate(anyvar);
setContentView(R.layout.sm);
browser=(WebView)findViewById(R.id.webkit);
WebSettings webSettings = browser.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
browser.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
browser.setWebViewClient(new WebViewClient());
{
browser.loadUrl("http://www.sm.ividhya.com/j128/");
}
}
}
try this DownloadListener example :
public class webActivity extends Activity {
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.webView=(WebView) this.findViewById(R.id.webview);
this.webView.getSettings().setSupportZoom(false);
this.webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
this.webView.loadUrl("http://www.sm.ividhya.com/j128/");
this.webView.setWebViewClient(new WebViewClientDemo());
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
});
}
private class WebViewClientDemo extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
package com.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
import android.content.Intent;
import android.net.MailTo;
import android.content.Context;
public class HelloWorld extends Activity
{
final Activity activity = this;
WebView webview;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (webview != null && (keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://www.blahblah.org");
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webview.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.startsWith("mailto:")){
MailTo mt = MailTo.parse(url);
Intent i = newEmailIntent(activity.this, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
startActivity(i);
view.reload();
return true;
}
view.loadUrl(url);
return true;
}
});
}
public static Intent newEmailIntent(Context context, String address, String subject, String body, String cc) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
intent.putExtra(Intent.EXTRA_TEXT, body);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_CC, cc);
intent.setType("message/rfc822");
return intent;
}
}
After help from people and posts on this site I was able to get a functioning web app that works. I just need to get the "mailto" functioning and I'm good. I have tried different code from posts I found. The above code is getting "IntentSupport cannot be resolved" and "MIME Type cannot be resolved" Just started trying to learn this stuff. Any feedback would be great. Thanks!
What is the IntentSupport class? I don't see you importing it any where. I have a feeling like it's some third party library that you don't have installed.