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;
}
Related
I am using tawk.to to insert an online chat in my app, with a simple WebView to load the chat, but when I put the url of tawk.to it does not display something. With any other url it works fine and loads the page, but not the chat. I also tried the page 'tawk.to' and it loads.
public class ChatOnlineActivity extends AppCompatActivity {
WebView chatOnlineWebVIew;
ProgressBar loadingChat;
/*MORE CODE HERE*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_online);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String url="https://tawk.to/chat/597771645dfc8255d623ede4/default?fbclid=IwAR3QB_DOoEu9ePTSPJbayOcTIaWtXjuJMUn46qLFTRVNnpPZBTZhsOy6lrs";
//String url="https://tawk.to/"; //JUST TEST URLS
loadingChat=(ProgressBar)findViewById(R.id.loadingChatOnline);
chatOnlineWebVIew = (WebView) findViewById(R.id.chatWebView);
WebSettings chatSetting=chatOnlineWebVIew.getSettings();
chatSetting.setJavaScriptEnabled(true);
//chatSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
chatOnlineWebVIew.setWebViewClient(new chatWebClient());
//chatOnlineWebVIew.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
chatOnlineWebVIew.loadUrl(url);
/*MORE CODE HERE*/
private class chatWebClient extends WebViewClient{
#Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
loadingChat.setVisibility(View.VISIBLE);
return super.shouldOverrideKeyEvent(view, event);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
#Override
public void onPageFinished(WebView view, String url) {
loadingChat.setVisibility(View.GONE);
chatOnlineWebVIew.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(),url,Toast.LENGTH_LONG).show(); //just massage to test if the urls is correct
super.onPageFinished(view, url);
}
}
}
I was also facing the same issue, my WebView was loading the url but not displaying but now i have fixed my issue by adding these following lines of code
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
and my final activity is look like below
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.jetbrains.annotations.NotNull;
public class TawkToChatWebView extends AppCompatActivity {
private WebView webView;
Activity activity ;
private ProgressDialog progDailog;
#SuppressLint({"NewApi", "SetJavaScriptEnabled"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tawktochat);
if (getSupportActionBar()!=null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
webView=findViewById(R.id.wv_chat);
activity = this;
progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
progDailog.setCancelable(false);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setSupportZoom(true);
webSettings.setDefaultTextEncodingName("utf-8");
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();
Log.e("loading","finished");
}
});
webView.loadUrl("ADD_YOUR_DIRECT_CHAT_LINK_HERE");
}
#Override
public boolean onOptionsItemSelected(#NonNull #NotNull MenuItem item) {
if (item.getItemId()==android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}
I have this code, but not because it works, it keeps opening in webview and what I want is that the links do not belong to my website open in your default browser. Any idea? thanks
here is my MainActivity.java
package club.moviestreet.www.webviewapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings= mywebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebView.loadUrl("http://moviestreet.club/");
// Line of Code for opening links in app
mywebView.setWebViewClient(new WebViewClient());
}
//Code For Back Button
#Override
public void onBackPressed() {
if(mywebView.canGoBack())
{
mywebView.goBack();
}
else
{
super.onBackPressed();
}
}
}
What you need is shouldOverrideUrlLoading of WebViewClient
Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url.
Replace
mywebView.setWebViewClient(new WebViewClient());
With this:
mywebView.setWebViewClient(new WebViewClient() {
#SuppressWarnings("deprecation")
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!url.startsWith("http://moviestreet.club/")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
}
return false;
}
});
I have webview included in my app. I want to navigate webpage history, but I don`t know how to do that. Please help me. My Main.java below:
package com.abc.test;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wv = (WebView)findViewById(R.id.webview);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
wv.loadUrl("http://www.google.com");
}
}
Here is the code
public void goBackInWebView(){
WebBackForwardList history = webView.copyBackForwardList();
int index = -1;
String url = null;
while (webView.canGoBackOrForward(index)) {
if (!history.getItemAtIndex(history.getCurrentIndex() + index).getUrl().equals("about:blank")) {
webView.goBackOrForward(index);
url = history.getItemAtIndex(-index).getUrl();
Log.e("tag","first non empty" + url);
break;
}
index --;
}
// no history found that is not empty
if (url == null) {
finish();
}
}
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.
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);
}
});