Enable download ​from webview in Sketchware without code Injection

 It has been a while since the discovery of code injection technique in Sketchware and the code for enabling download from webview in Android has been known ever since.

But now I have discovered a way to enable download from webview in Sketchware, without injecting any external code or without rebuilding the app code with any other software. Given below is the step by step process.

1. If you are not using Sketchware, download it from the link provided. It is a great android app through which you can create you own android apps right from your mobile. Download Sketchware.

2. Start a new project in Sketchware.

3. In the view area, insert a Scroll(V) with layout_width set to match_parent. Inside the scroll(V) layout, insert a webview widget.

* I found later that not using Scrollview gives better results. WebView outside scrollview can be easily scrolled vertically and horizontally.

4. In Logic area of the project add the following events:
onBackPressed
WebView: webview1 onPageStarted
WebView: webview1 onPageFinished

and also add an Intent component:
Intent download

5. Now go to onCreate event and add three string variables namely a, b, and c.

6. In onCreate event add the WebView loadUrl block and write the initial url to be loaded.
7. In onBackPressed event use following blocks:
If WebView webview1 canGoBack then
WebView webview1 goBack
else
FinishActivity
8. In the event When webview1 pageStarted (url), add the following blocks:
set a to WebView webview1 getUrl
set c  to (url)
9. In the event When webview1 pageFinished (url), add the following blocks:
set b to WebView webview1 getUrl
If (a equals b) and not (c equals b) then
Intent download setAction ACTION_VIEW
Intent download setData c
StartActivity download

10. Now save and run the project. You will be able to download when you click on download links in your app.

Here is a video for better understanding of the logic:


If you like this then you are requested to share it. Also subscribe to my YouTube channel for more such logics.

Comments

  1. Hello! what of in the case of allowing your webApp to upload files when upload the upload button is pressed (upload in your website)

    please reply me here studyout2016@gmail.com

    ReplyDelete
  2. Do we have to do the "If back pressed" thingy?

    ReplyDelete
    Replies
    1. The code in onBackPressed makes you go back to previous page in browse. If you don't add onBackPressed, app will exit on pressing back button.

      Delete
    2. so can we have a button (not the back button) to do the job of the onBackPressed do we still have to have the onBackPressed?

      Delete
    3. also when you do this non code ingestion way it actually opens those pages in the browser in the background

      Delete
    4. Using codes gives better results.

      Also, it is better if you don't use Scrollview because webview itself is scrollable. Using scrollview hinders scrolling it laterally.

      If you don't use onBackPressed code, it finishes Activity on back pressed.

      You can use the code used in onBackPressed in any other event, like on Button Click or on Imageview Click or Textview Click.

      Delete
  3. Nice....!!!Give Us More Tutroils..
    And Request Video Is How To Share And Open any other link:email,sms etc on webview

    ReplyDelete
  4. Hi what I'm trying to do is to add an apk file to your sketchware application like images

    ReplyDelete
    Replies
    1. You can add app file in assets folder, using same method as this: https://youtu.be/SKRG9hEZS3k

      Delete
  5. How to enable search of word in webview.

    ReplyDelete
  6. Action view can not resolved to a varible.. What to do??

    I follow exatly the same as u shown...

    ReplyDelete
    Replies
    1. That is Sketchware bug. Download latest version of Sketchware.

      Delete
  7. Does this still work? I tried the code but I can't download a pdf file.

    ReplyDelete
  8. Code is very bad. Why you should download in another app. You can download from your own app. Copy this code:
    webview1.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
    DownloadManager.Request request = new DownloadManager.Request( Uri.parse(url)); request.setMimeType(mimeType); String cookies = CookieManager.getInstance().getCookie(url); request.addRequestHeader("cookie", cookies); request.addRequestHeader("User-Agent", userAgent); request.setDescription("Downloading File..."); request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType)); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir( Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName( url, contentDisposition, mimeType)); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); dm.enqueue(request); Toast.makeText(getApplicationContext(), "Download started", Toast.LENGTH_LONG).show();
    }});

    ReplyDelete
    Replies
    1. Your code requires app to request for storage permission before it works

      Delete

Post a Comment

Popular posts from this blog

A Flash Light App in Sketchware

Create Music Player app in Sketchware

Simple car racing android game in Sketchware

In Sketchware: How to create a calculator for any simple formula?

Firebase auth in Sketchware for Login Activity