So now that you have set up the project, lets start coding and making this File Explorer come to life! (If you haven’t read the first part of this series, please read it here.)

Setting up FilesListFragment

The first thing you are going to do is create a Fragment named FilesListFragment. In this fragment you will take a path as an argument, read all the files/directories on that path and show them in a RecyclerView.

Here is the layout file for FilesListFragment. As you can see it has a RecyclerView and Layout that we will show when a folder is empty and doesn’t have and files in it.

Sprinkle Kotlin Magic

In the app.gradle file make sure you have added the kotlin-extensions plugin.

We will use Builder pattern in the Fragment to instantiate its objects.

Go ahead and a new Builder class inside FilesListFragment.

In the companion object of FilesListFragment add a build function that takes in an extension function on Builder applies it on a new Builder instance and calls the build, method to return the instance of FilesListFragment.

The concept is relatively simple here, the build functions takes an extension function on Builder so you have access to all variables in the Builder class, then you create a new Builder instance and apply this function on it, which will set the variables to whatever value was assigned in the extension function. These are also known as receiver functions. Lets build and set this Fragment in MainActivity.

Initially the path will to the user’s public directory. Below is the layout file of MainActivity.

Your application might crash when you run it, because we haven’t yet request file permission on runtime, we will do that later, for time being go to Settings > Apps > Kotlin File Explorer > Permissions, and grant the files permission. Re run this app, it should now work perfectly.

Reading files at path

Before we start reading files, lets do some set up!

In a folder named common, create a new enum named FileType. This enum will help us denote the file type of current file, whether is a directory or a simple file.

Create a new data class named FileModel, this class will contain all the data we require related to a file.

Now create a new file named FileUtils, this file will contain all of the code related to reading, creating, deleting, copying and moving files.

Let’s add a function to FileUtils that reads all the files from a path. You can do that simple create an instance of File with the given path and call listFiles() on it. We will also add two extra parameters, once to toggle the viability of hidden files(files starting with a ‘.’), and other to view just folder.

In the same FileUtils let’s create a function that converts List<File> to List<FileModel>. We will also add a function convert the file size of MB.

Never miss a post from TheTechnoCafe

Displaying files at path

Now that we are done getting a list of files, lets show them in the RecyclerView in FilesListFragment.

Create a layout file for a single recycler view item named item_recycler_file.xml.

Now create a adapter for the RecyclerView named FilesRecyclerAdapter.

As you can see we have set up click and long click listener for a single item which we will use in future tutorials. There is an updateData method that updates the content inside the adapter. In the ViewHolder’s bindView method you can see that when the FileType is Folder we have hidden the totalSizeTextView, reason is that android doesn’t actually return the exact size of folder, it works fine for individual files though. So we hide the size in case of FileType.Folder and show a normal TextView with text “Folder”.

Jump into the FilesListFragment and set up the Adapter for RecyclerView.

In onViewCreated we first check that there is a PATH provided in the arguments. Then in initViews we set up FilesRecyclerAdapter and in updateData using the functions created in FileUtils.kt we get list of File, convert then to list of FileModel and pass it to our FileRecyclerAdapter. We also check if the list of files is empty then emptyFolderLayout is shown.

Go ahead and run the application now, you should see a list of files and folders.


<< Previous Tutorial – Part 1 – Introduction and Set up.

Next Tutorial – Part 3 – Navigating through File System >>

Gurleen Sethi

How can I tell you about myself in just a few lines, to get to know me you can reach out to me, email, text, call, or any other method of communication you like. I am always there for you!


Jarra · October 9, 2019 at 12:52 pm

Hello, thanks for the share. Can i build the app, so it can explore my PC files over wifi? would that be difficult? thanks

Build a File Explorer in Kotlin – Part 3 – Navigating through File System - TheTechnoCafe · August 14, 2018 at 4:35 pm

[…] We want to listen whenever the user clicks on any folder or application in our MainActivity. If you have followed the series properly you might remember that we have already added Click Listeners on items in our FileRecyclerAdater in Tutorial 2 – Reading files from paths. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

16 + 11 =