To download a PDF using a link and set the progress value on a ProgressBar in Android, you can use the following steps:
- Add internet permission to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
- Add a ProgressBar to your layout file:
<ProgressBar android:id="@+id/progressBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" />
- In your activity or fragment, declare a variable for the ProgressBar and initialize it:
private ProgressBar progressBar; progressBar = findViewById(R.id.progressBar);
- Create a method to download the PDF from the link:
private void downloadPDF(String pdfUrl) { // Show progress bar progressBar.setVisibility(View.VISIBLE); // Create download request DownloadManager.Request request = new DownloadManager.Request(Uri.parse(pdfUrl)); request.setDescription("Downloading PDF..."); request.setTitle("PDF Download"); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "my_pdf.pdf"); // Get download service and enqueue file DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(request); // Set up a broadcast receiver to listen for when download is complete BroadcastReceiver onComplete = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { // Hide progress bar progressBar.setVisibility(View.GONE); // Unregister receiver unregisterReceiver(this); } }; // Register receiver for when download is complete registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); }
- Call the downloadPDF method when you want to start the download:
String pdfUrl = "https://example.com/my_pdf.pdf"; downloadPDF(pdfUrl);
- To set the progress value on the ProgressBar, you can use the DownloadManager’s query method to get information about the download, including the current progress:
private void setProgressValue(long downloadId) { DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); Cursor cursor = manager.query(query); if (cursor.moveToFirst()) { int bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); int bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); if (bytesTotal > 0) { int progress = (int) ((bytesDownloaded * 100L) / bytesTotal); progressBar.setProgress(progress); } } cursor.close(); }
- To continuously update the progress value, you can use a Handler and a Runnable to periodically call the setProgressValue method:
private Handler handler = new Handler(); private Runnable runnable = new Runnable() { @Override public void run() { setProgressValue(downloadId); handler.postDelayed(this, 1000); } }; private long downloadId; private void downloadPDF(String pdfUrl) { // Show progress bar progressBar.setVisibility(View.VISIBLE); // Create download request DownloadManager.Request request = new DownloadManager.Request(Uri.parse(pdfUrl)); request.setDescription("Downloading PDF..."); request.setTitle("PDF Download"); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment