Till now you have added functionality to just read and open files in the .In this tutorial you will add the features to create/delete files and folders in the files system.

Adding menu options to create

We will add the options to create a file/folder in the menu options of our toolbar. Below is how the application will look once the options are added.

kotlin kotlin

Go ahead and create a new menu file named main_menu.xml.

You can import the necessary icons used in the menu by right clicking on res -> New -> Image Asset. Or you can use any icons that you would like.

Inflate the menu in MainActivity.kt.

As we have added two options in the menu, one for creating folder and one for file, lets check if the users presses any one of them.

Before actually creating the File/Folder we first need the name of the new File/Folder being created. We will ask the user for this name inside a dialog. We will use the inbuilt BottomSheetDialog to get user input. First lets create a layout for the dialog. Create a layout file named dialog_enter_name.xml.

First lets complete the process of creating a new File by showing a dialog, getting the input and creating the file.

Now let’s create a new function in FileUtils.kt named createNewFile that will create a new file.

The functions takes in file name, path and a callback which will be called on successful/unsuccessful creation of file. We first check if a file with provided name already exists, and if so then we notify using the callback. If not, then we try to create the new file and notify the result using callback. Let’s use this function in the dialog.

This is how the dialog will look like.


Notifying file create changes

The file will be created successfully. But there is a problem. The current fragment hasn’t been notified that the list of files has been updated (a new file has been created). We can do that by directly accessing the current fragment and calling functions on it. But this is not a good approach. We will use Broadcast Receiver for this. Go ahead an create a new BroadcastReceiver named FileChangeBroadcastReceiver.

As constructor arguments this receiver will take a path and a onChange listener. Whenever files at a certain path are changed, a broadcast for that path will be sent, and any receiver listening for that particular path will receive the event and invoke the listener.

Register/Unregister this receiver in FilesListFragment.

Now when there is any change at any path, all we have to do is send a broadcast.

The process of create a new Folder is exactly the same. First create a function named createNewFolder in FileUtils.kt.

As you can see this is almost same as the createNewFile function. Now let’s show the dialog and call this function in MainActivity.kt.

Never miss a post from TheTechnoCafe

Deleting Files

When the user long presses on a folder or a file we will show an options dialog which will contain a delete button. So first set up the dialog. Create a new file named FileOptionsDialog.kt.

In this case we will extend BottomSheetDialogFragment. Create a on delete listener and invoke it when the button is clicked. Below is the layout file for the same.

Now let’s open this dialog when user long presses on a file or folder. Do this in onLongClick in the MainActivity.

Create a function in FileUtils.kt to delete a file/folder from a path.

We first check if its a folder then delete recursively, and if its a file then use the delete function.

Note: Android already has a function name deleteFile in the Activity scope so we need to use a named import for this function.

This is how the dialog will look like.


That is it for this tutorial. It was a long time. In the next tutorial, you will see how to copy and move files from one path to another.

<< Previous Tutorial – Part 4 – Adding Breadcrumbs.

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

Fauzie · May 15, 2019 at 10:56 pm

Please continue this series.

Leave a Reply

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

18 − 10 =