Open In App

How to Build PDF Downloader App in Android with Kotlin?

Last Updated : 31 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

If you are creating apps for students or for educational purposes then you need to add some PDF files for displaying some data inside our app. These PDF files are updated on regular basis. A sample video is given below to get an idea about what we are going to do in this article.

In this article, we will build an application to help users download notes in pdf format.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Add Internet Permission in the AndroidManifest.xml file

XML




<uses-permission android:name="android.permission.INTERNET"/>


Step 3: Create Firebase Project 

Although we are not connecting firebase with the android studio we will be using it as a medium of storage. Go to the firebase website and click go to console and create a new firebase project. After that go to firebase storage here you will get the option to upload pdf files click on that upload button in order to upload files from your system. Click on the pdf uploaded by you in order to generate a pdf URL like this.

Step 4: Design layout Files

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. In activity_min.xml we are just adding a RecyclerView. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
   
</androidx.constraintlayout.widget.ConstraintLayout>


Since we are using recycler view we need to create a new layout file for that go to res->layout and create a new Layout resource file named item_layout.

item_layout file

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_margin="5dp">
    
    <ImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pdf_image"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:src="@drawable/pdf"/>
    
    <TextView
        android:id="@+id/subj_name"
        android:layout_width="match_parent"
        android:layout_height= "match_parent"
        android:text="System Design Notes"
        android:textSize="20sp"
        android:textAlignment="center"
        android:padding="30dp"
        android:textStyle="bold"
        android:textColor="@color/black">
    </TextView>
  
</LinearLayout>


Step 5: Create Notes class 

Kotlin




class Notes 
    ( var image : Int
    , var subj_name : String
    ,var url : String?
    )


Step 6: Create NotesAdapter class

Since we are using recycler view therefore we will create an adapter class.  We will also use the concept of Download Manager in order to download notes in pdf format.

Kotlin




class  NotesAdapter( var data: ArrayList<Notes>, var context: Context) : RecyclerView.Adapter<NotesAdapter.ViewHolder>() {
  
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layout = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false)
        return ViewHolder(layout)
    }
  
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.name.text = data[position].subj_name
        holder.image.setImageResource(data[position].image)
        holder.itemView.setOnClickListener{
            var download= context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
            var PdfUri = Uri.parse(data[position].url)
            var getPdf = DownloadManager.Request(PdfUri)
            getPdf.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
            download.enqueue(getPdf)
            Toast.makeText(context,"Download Started", Toast.LENGTH_LONG).show()
        }
    }
  
    override fun getItemCount(): Int {
        return data.size
    }
  
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        internal val name: TextView
        internal val image: ImageView
        init {
            name = itemView.findViewById(R.id.subj_name)
            image = itemView.findViewById(R.id.pdf_image)
        }
    }
}


Step 7: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val item = ArrayList<Notes>()
        item.add(
            Notes(
                R.drawable.pdf,
                "System Design",
            )
        )
        item.add(
            Notes(
                R.drawable.pdf,
                "DBMS",
                "https://firebasestorage.googleapis.com/v0/b/online-learning-ea8c0.appspot.com/o/Uploads%2Fdbms_tutorial%20(1).pdf?alt=media&token=7e8a8500-5c94-4b03-a088-3b071d8b35e1"
            )
        )
        item.add(
            Notes(
                R.drawable.pdf,
                "C Notes",
            )
        )
        item.add(
            Notes(
                R.drawable.pdf,
                "Resume",
            )
        )
  val recycler_view  = findViewById<RecyclerView>(R.id.recycler_view)
        recycler_view.layoutManager = LinearLayoutManager(applicationContext)
        val adapter = NotesAdapter(item,this)
        recycler_view.adapter = adapter
    }
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads