Welcome to the 3rd part of the Kotlin File Explorer series. Good job reading files from a path in the previous tutorial. Right now our application takes a single path and displays the file/folders at the path. We want to make our application more interactive. Clicking a folder should display the content in that folder and clicking a file should open it in appropriate application.

Setting up Click Listeners

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.

We will now add listener in FilesListFragment to pass on this information into MainActivity. Go ahead and create an interface named OnItemClickListener in FilesListFragment. Also create a corresponding lateinit variable for the same. lateinit in Kotlin is a great feature, it lets you define a non null variable telling Kotlin compiler that the variable will get a value which will be non-null before the variable is used, but make sure the variable gets a non-null value before usage, else your application will crash.

This interface will be implement by the MainActivity. In onAttach lets assign mCallback a value.

In onAttach we get the reference of our MainActivity, we cast it to OnItemClickListener, if the cast fails we through an exception and crash the application. Now let’s implement this interface in MainActivity.

Adding new Fragment to stack

In onClick, if its a folder we want to create a new FilesListFragment with a new path and add it on top of our existing fragment, if its a file we want to send an intent to open the file in appropriate application.

In MainActivity, create a function named addFileFragment that will create a new FilesListFragment with a provided path and replace the current fragment with the new one.

We create a new fragment from the provided path, replace the current fragment with new one and this fragment to back stack since we want to show the previous fragment if user presses the back button. In onClick, check if the FileType is Folder then call this function.

Now lets handle when user presses back.

When there are no more fragments on the stack, close the activity.

Never miss a post from TheTechnoCafe

Opening files on click

Opening files is very simple. All you have to do is fire an Intent with proper data in it. Let’s see how!

In the FileUtils.kt create an extension function on Context named launchFileIntent.

Intent.data is set to the Uri of the File that you are trying to share. We add a Intent.FLAG_GRANT_READ_URI_PERMISSION so that other applications can read the file from the Uri, if you don’t do this, other applications won’t be able to read the file.

Now call this function in onClick.

In this next tutorial you will create breadcrumbs for easier navigation.

<< Previous Part 2 – Reading files from paths

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!

1 Comment

Marek Šabo · October 12, 2018 at 6:35 am

There is a missing code in FilesListFragment#initView():

mFilesAdapter.onItemClickListener = {

mFilesAdapter.onItemLongClickListener = {
Taken from: https://github.com/gurleensethi/kotlin-file-explorer/blob/master/app/src/main/java/com/thetechnocafe/gurleensethi/kotlinfileexplorer/fileslist/FilesListFragment.kt

Leave a Reply

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

11 + seven =