Android File Manager App Creation

Hello, and welcome to this Android development tutorial. This is part one on how to make a file manager application.

If you will enjoy reading and contributing to the discussion for this post, will you please join us on the YouTube video above and leave a comment there because I read and respond to most comments on YouTube?

If you find anything helpful in this video or funny, will you please leave a like because you will feel great helping other people find it?

We open Android Studio then we click on start a new Android studio project. We select empty activity and click on next.

As the name we will change it to File Manager. We will make sure that Java is set as language and the minimum API level is at least 14. We will click on finish and we will wait for the project to be loaded.

Android File Manager App Creation

This file manager will enable us to search through directories and files and different file types. We’ll be adding features and improving the UI and so on. While the project loads, we can say that this project will be very, very interesting because we will learn how to use ListViews which are kind of scrolling views in which we will display all your files, and folders, and all that kind of stuff. We will use also Java logic and mainly everything will be done in Java or in XML. 

Android File Manager App Creation

The first thing we will do is open the res folder under the layout folder, we will find the activity_main.xml file. We will right click on it and we will click on delete.

We will uncheck this if you have them checked and then we will click on okay.

Right now we will go and right click on layout, new and layout resource file, and we will create a new file.

We will call it layout1. The root element of this layout will be linear layout and we will click on okay.

Android File Manager App Creation

Right now, we will switch the design view to Textview and we can open the preview right here in order for us to know what’s going on i n the UI of our application.

Here we can start designing this UI we have, so if we have a file manager we would like to have a place in which we can show the files and folders, and the most easiest way to do it is with a ListView. So we will do that a little bit later but now we will just create kind of the architecture of this file manager.

For example, if we create a Textview right here, so we type Textview with match_parent. height can be maybe 15 DP, and we want to do with this something very simple. For example we set text to File Manager, so this will basically just display the title of our application. Now, we will increase the text size by specifying the text size attribute to 25 SP, and we will center the text in the Textview by specifying the gravity to center.

Android File Manager App Creation

We can change the background of the view if we want. For example, if we want to create a black background. First we will create a color for this.

For that, we go to the left side, we open the values folder, and under that, we will find the colors.xml file. We open this files you can see there is already some colors created by default, but we will add a new color.

We will type new element color and we will create a black color. So we type black. We close the element and inside the element we type number sign or hash tag, whatever you want to call it, and six zeros. We will follow the hash tag with six zeros. Now we have defined the black color.

Android File Manager App Creation

Now if we go back to layout1.file we can set the background of this Textview to black, and the background is black.

Now, we want to define a kind of white color, not that white, but we will call it light color. So were going to create a new color and name of the color will be light or just light color. Maybe we can specify it as LightColor and the value of this color will be six B’s. You can go and do it with uppercase because that’s kind of more readable if you like but it doesn’t really matter. So six B’s. Now we will set the text color attribute to light color and we can see that our file manager has a light color.

If you want to make this white kind of text light color a little bit more brighter or more whiter we can change the D to an E, for example. So we will go and type six Es instead of six Ds. I think that’s good for now. We now have the title of our file manager.

Next we will create a view that will simulate how our file manager will look like. We can go and say ListView, for example, which will be match_parent, the height will be 0 DP and the weight will be 1.

As you can see on the right side we have item one, sub item 1, item two sub item two and so on. This is the ListView that we have created right here. Here we will display the file names and so on.

Android File Manager App Creation

So we have our ListView ready and we have to create something else maybe a bottom bar that will enable us to have some buttons and actions. For that we will create a linear layout right here and width will be match_parent the height will be 50 DB. We can change this later. The orientation of the layout will, of course, be horizontal because we want  to create a horizontal bar at the bottom. So as you can see here we have a horizontal bar, a ListView and on top a TextView that says this is our File Manager.

Android File Manager App Creation

In the future we can add more features and even add buttons in the top where file manager title exists, but here, for example, we will create some buttons. So we will open an element. We will type button. The width will be 0 DP. The height will be match_parent and the weight will be 1. We can and change or copy this.

Now we have this button right here and we can enable an ID. For example, I will call it B1, but for me it means button one. We can add more buttons. For example, we will copy this and below it we will paste it maybe four times more, so we can have five buttons on our button bar, and we will change, of course, the ID of every one of them. B1 the next will be B2, the will be B3, the next B4 and the next B5. We can press control+alt+L to order and reformat the code to make it look nicer.

Android File Manager App Creation

Here we have five buttons at the bottom of the screen. We have our list view and we have our title.

We have all of these buttons. What we will do now, we will open our main_activity.java file and we will fix this error that appears here, of course, because we changed the name of our layout file. So we will instead type R.layout.layout1, and that will fix the error we have.

Android File Manager App Creation

We will create the buttons. We will get access to the buttons we created. We can also do it with the ListView but we will do it first with buttons. We will type final Button and we will, of course, import the button library and we will call it B1 is find ViewById(R.Id.B1). We will do the same for the other five buttons. We can copy this and paste five times. We have five buttons and here we have B1, this is B2, this is B3, B4 and B5. On the IDs we also have to change the IDs to be B2, B3, B4 and B5. We will set a click listener maybe to B1 just to test everything is working fine. We will say b1.setOnClickListener(view OnClickListener). We will say b1.setText(“Pressed”) just to make sure everything works fine.

I will run the app on my emulato. If you want you can do it on your emulator or your real device if you want. I’m compiling. As you can see Gradle build running and will be sent to the emulator if it doesn’t find errors in the code at compilation time.

Android File Manager App Creation

Right now, we will switch to the emulator to see if everything works as expected. Here you can see my emulator that it actually opened in the settings folder and settings app of the Android. I am running Android PI on this emulator and as you can see we have our file manager. In the bottom we have five buttons.

If we click on any but the first nothing happens but if we click on the first, the first says pressed.

We can see right here that we have two titles. The one that we created is the one that is below the other because it is centered the way we created it. The other is kind of the default. We don’t want that title because we cannot really add a lot of customizations to it, so we want to keep only the one we created.

Android File Manager App Creation

To hide the one that comes by default, we go to the values folder and open this styles.xml file. Here we can modify this style or the theme of our application. To do that we will create an item and we will set it to window no title, and we will set the value of that item to true.

So if we run the app again, we can see that the title will be gone. It’s loading right now so wait a second and now we have only the title we wanted.

Maybe we want the app to be full screen. We don’t want the top bar which contains the time and these icons. we want to hide that. To do that we will create another item right here that will say windowFullscreen. We will set that to true. We will run the app and see if it worked. We’re running the app and as you can see the top bar is not shown anymore.

Android File Manager App Creation

If it didn’t work for you don’t worry we will do something that we’ll fix on every phone, but it is probable that it worked.

We have our file manager title. We have our ListView right here, and we have our five buttons on the bottom of our layout. We will go to our layout again and we will check something else right here. I told you that we didn’t access the ListView from java because the reason for that is the ListView doesn’t have an ID, so we’ll create an ID for the ListView, and we will call it ListView and here we can close this, so that it looks better.

Android File Manager App Creation

We go now to main_activity.java and after this onClickListener right here we will create our ListView. We’ll access the ListView by saying final ListView and we will name it ListView. We will say findViewById(R.id.ListView). Now we have the list view right here and we have accessed the rest of the buttons right here. Later, we can maybe rename this buttons. Maybe B1 can become go back to the previous folder. B2 can be maybe something like rename the folder or something and so on and so forth.

Part Two Of How To Create A File Monitor App For Android.

In the previous part we learned how to create the UI elements. We created the title, we created our ListView in xml, we created some buttons and we initialized some buttons and listener, and ListView.

In this tutorial, we will actually access our ListView and start changing the values in it to learn how a ListView works and all of that. The first thing we will do is we will go to our layout folder, right click new, and click in layout resource file.

Android File Manager App Creation

Here the name will be item because we are defining the item kind of the rectangle of the ListView, and the root element will be a Textview because we will display text on every item. We will click on OK.

As you can see right now, I can let you see that here we have a Textview auto generated by the IDE which says width match_parent and height match_parent. We don’t need the last closing bracket so we’ll close it here, and we will delete this. I just typed this forward slash right here.

We have to give this item an ID. The ID of this item will be, for example, textItem. We can maybe put the height we want right here for this item. For example, 50 DP, and we can also specify the color which will be black, and the padding which will be maybe 6 DP.

Android File Manager App Creation

All of this are just cosmetic changes but the one  thing you must have is the ID. Once we created this, we will create a text adapter. An adapter class that will enable us to set the information on the items on the ListView. For that we will say class TextAdapter extends baseAdapter. As you can see here it tells us that we have to implement method, so we click on that red warning sign, and we click on implement method, and then okay.

What just happened was the IDE auto-generated for us some method. The one thing we will change in this method right now, in the second method it’s overwrite public object getItem. We will change that from object to string because we will be using strings on our text fields.

We have our text adapter right here and so we now can connect the text adapter with our list view. To do that we say final TextAdapter, we can say textAdapter1, and we will set this to new TextAdapter().

Android File Manager App Creation

We have our new text adapter and we can set listView.setAdapter to textAdapter1.

Android File Manager App Creation

That’s okay. We have to modify our text adapter to fit our needs. To do that we will say private List<String>. We will call this data. We will set it to new ArrayList<>. As simple as that. We will create a new element that will maybe be useful for us because we will have to set data so we will create a method. Public void setData(List<String> data). Inside this setData method, we will check first if data is not null. So data is different than null then we will clear the data of the textAdapter. To access that we will say this.data.clear(). Now, we will say if data.size is greater than zero we’ll say this.data.addAll, and we’ll say data. As simple as that.

Always remember to end the line with a semicolon in Java. After this, we will tell the adapter that we have just updated the data. We want the adapter to reflect this change on the screen. For that we call the notifyDataSetChanged method.

Android File Manager App Creation

The next method is getCount so we will modify it to our needs which will be in our case, return data.size. As simple as that.

We won’t use this getItemId just yet. We will go to getView and we will create a viewholder class outside this things. Outside the getView, after the getView method we will say class ViewHolder – which is kind of the object we used to refer to what it’s holding in the view – and it will be TextView info, for example. The constructor will be viewHolder(TextView), the parameter of TextView will be info. Inside this constructor we will say this.info assigned to info.

Android File Manager App Creation

We can go to our getView and instead of returning null, we will return of course the view. In this case, the view will return will be called convertView as it is passed with the parameter. Inside this, we will first check if convertView is null. So say if convertView equals null then we have to kind of initialize convertView and set all this to be correct. We say convertView is equal to LayoutInflater.from. So we’re inflating this layout from parent which also is a parameter, and, of course, want to get the context of the parent. Then we say outside this, we will type inflate and we will inflate the item layout which was the text item we created, so R.layout.Item. We also have to pass the parent to complete the passing parameters. I will say convertView.setTag which sets the information right here to new ViewHolder which is the one we created recently. We will cast ViewHolder to TextView then we will cast it in convertView.findViewBy.Id. In this case, we type the ID of the text we created which is textItem.

All of this is in case the convert view object is null, After the null where the if ends we will create a ViewHolder to define the view we are returning, right? Holder will cast this to ViewHolder and this is convertView. We’re getting the view from convertView, So say getTag just like that. Now, we say final string Item and we pass the position to get the item actually.

Android File Manager App Creation

If we’re getting the item, we have to return the item. So in the get item method right here we are returning null, so that would cause an error, of course. You have to return data.get(position). This way the crash won’t occur.

We go back to final string item right here and we will say holder.info.setText. Here we were actually updating the text of the item and this will be item. This is all that we need really in this adapter.

Android File Manager App Creation

We don’t really need anything else. If we need something else we’ll do it later. We will create programmatically a ListView and update the information of the ArrayList.

To do this we will say List<Strings> and say example because I’ll just call it example right now and this will be new ArrayList(). Now, we will create a for loop in order for us to fill the array list just for testing purposes. To test if everything is okay. So int i = 0: i <100; i++. Here we will add a string. We will add a string.value(i). As simple as this.

Now, we will run the app and we will see if it’s working fine. It should show our list view with all the elements we just added. I’m opening the emulator and something is not working as we expected. Maybe something is wrong right here.

Oh, because we are not adding the stuff to the parameters to the adapter. We fill this string array list but we still didn’t send that to the text adapter. So we say textAdapter1.setData and we’ll set data to example.

Android File Manager App Creation

It should work. So we run the app again. We wait for the compilation to end and we have an error. What’s going on? We will see. Maybe we typed something incorrectly right here. We forgot to add a false thing right here which is attached to root. So after the parent we pass an extra parameter which is false.

Now we will hopefully run again and this time it will work. We’re running it and yes, this time it works. As you can see right here we start from zero to 1,2,3,4,5,6,7,8,9, all the way down to  99 I think because we started the counter from 0. So 99. There you can see that we have our ListView working really, really fine.

Android File Manager App Creation

Part Three Of How To Create A File Monitor App For Android.

In the previous tutorials we learned how to create a layout as you can see right here, and we also set up the text adapter, the ListView adapter and we displayed a hundred elements on the screen.

In this tutorial, we will learn how to setup up the permissions to read and write to the storage of the device. We also will actually learn how to get a list of the files of a directory and start displaying them on our file manager application.

The first thing we need to do is to specify the permissions we need. We go to our manifest file under the manifests folder and we open an element and type users permission and type storage. READ_EXTERNAL_STORAGE will be the first permission that we will use, and the second one will be WRITE_EXTERNAL_STORAGE. Please check that you have these two lines exactly as I have right here. I am selecting them in order for you to see.

Android requires us to specify the permissions we need and after Android version 6 which is Marshmallow, we have to request permission usages at runtime. We will also learn how to do that. After the TextAdapter class, we will override the onResume method.

Protected void onResume. We open the method then we type super.onResume. So every time this method is called we will check if we have permissions. Because remember if you go to settings on your Android device you can disable the permission access from certain apps. That’s why we have to check every time the app is resumed if we still have those permissions.

Android File Manager App Creation

Of course, the first time we will request them and the later times we will check if we still have them. To do that we will create a code kind of an identifier for permission request. We type private static final int and then and we say REQUEST_PERMISSIONS and that we will set to a number. For example it can be any kind of identifier. It doesn’t really matter. For example, 1234. It doesn’t really matter.

Now we define a string array with the actual permissions we want to request. We will say private static final String[] and in caps PERMISSIONS actually because we will be asking for two permissions. We then say say Manifest.permission.READ_EXTERNAL STORAGE and create a comma right here, and Manifest.permission.WRITE_EXTERNAL STORAGE. If we use this way, I think this is a good way of defining permissions because we can add more permissions.Just type comma and then add more permissions.

Android File Manager App Creation

As you can see here we have a warning right here which says field requires API level 16 current minimum is 14. So to fix this we will go an increase our API level to 16.

We go to the left, then open the Gradle script, and under module app – which is the second build.gradle file we open this file and we search for minSdkVersion which is the minimum SDK version our app will target. I will change this from 14 to 16 because 14 is too old. So we’re going a bit up to 16.

We click on sync now and we wait for the app to be synced. If you don’t find sync now button you can also click on this top bar for this elephant button and everything will work fine. Every time you modify this file you have to do just that.

Android File Manager App Creation

The warning has disappeared. We will also create another field, private static final int that will be about permissions count. So PERMISSIONS_COUNT. In this case we are asking for two permissions, so we’ll set it to two.

On the onResume method we will later check if we have permissions or not . We will create a boolean that will enable us to do this. The boolean method actually.

So the boolean method will be private boolean arePermissionsGranted, for example. In this way we will know if the permissions are granted or not. Here as I told you, we only have to do this if we are on version 6 or Marshmallow or above. To do that we say if Build.VERSION.SDK_INT is greater or equal then Build.VERSION_CODES.M. M stands for Marshmallow. So this way we’re saying if the device is running Marshmallow or higher then we have to ask for permissions.

We create an int and we call it P, for example, and we will set it to 0. Here we say while P is less than PERMISSIONS_COUNT, we will ask for a permission or we check for permission. To do this we say checkSelfPermission and we will pass our PERMISSIONS P because this is the pointer actually. If this is different than PackageManager.PERMISSION_GRANTED, this means if the permission is not granted we will return false, of course. So we return false.

Android File Manager App Creation

We don’t want this to happen, right? Because we’re returning true only when one permission is granted. We will change this a bit and we will say arePermissionsDenied or something. That’s better looking, better logic. This means we are returning true.

Otherwise, we return false because this also applies for versions that are below Marshmallow, and if this didn’t return true this means that permissions are actually not denied. Which means are granted.

GET MORE OF THIS COURSE

Would you like to continue learning about the Code an Android File Manager and Explorer App in Android Studio Today Course? If you are interested will you please buy the complete course, Code an Android File Manager and Explorer App in Android Studio Today Course, on the Uthena Education Platform..

You can also get the first two hours of the course completely for free on YouTube.

Thank you for reading the blog post or watching the course on YouTube.

You may like to read Build an Android Studio Gallery App Like QuickPic Google Photos FOTO Piktures A+ and Camera Roll and Code Your Own Video App Player in Android Studio!

I love you.

You’re awesome.

Thank you very much for checking out the Code an Android File Manager and Explorer App in Android Studio Today Course and I hope to see you again in the next blog post or video.

Love,

Jerry Banfield.