Android. Create a file manager

Android - a promising and rapidly growing operating system. And many programmers want to learn how to program applications for OS Android, but a quick search of structured materials in the Internet and, in particular, Lesbian, leads them into a stupor. Indeed, there is still a problem finding tutorials (especially in Russian) to develop applications for this very popular operating system.
Well, we will gradually improve the situation with Habra.
I shall warn that the materials are intended for those who do not have experience in developing applications for Android, but very, very much wants to get this experience.

Getting ready

The first thing we need - to install and configure the IDE. We will use Eclipse, but it is important to note that it is possible to use any development environment.
Detailed instructions for installing and configuring IDE Eclipse is contained in the documentation for Android site .In brief:
  • Install Eclipse Classic from here
  • Install SDK Starter Package here
  • Set ADT Plugin using this manual , for the same instructions perform the steps described in the section Configuring the ADT Plugin
  • Launch Eclipse, click Window> Android SDK and AVD Manager and select the necessary components for installation. The easiest option - to select all but the bare minimum - SDK Platform Android 2.1 (or 2.2), Android SDK Tools, Android SDK Platform-tools

After all this we have an operating environment for developing applications for OS Android. But we need a place to test our application, right? And here we face a choice - either to perform these actions in the emulator or on a real device.
The emulator can be added to the familiar box, Android SDK and AVD Manager, clicking the New ... tab inVirtual Devices. Fill in the field Name, specify the version of the API and add our virtual device.

If you want to test their applications on a real device, see here . It is important to note that in the end you still have to verify its application on a real device, because a little strange to release the application, for example, in the Android Market, as it should be no testing the samples at the existing phone / tablet.

Create Project

It's time to create a new project. To do this, click File> New> Project, select the pop-up window Android> Android Project and click Next. The following window appears, which we fill like this:

Now a little explanation.
Project Name - name of the project in Eclipse.
Application Name - the name of the application, the one that users will see on the final Android-device.
Package Name - the name of the package. Everything is just like any Java-project. It is important to know that the name must be unique among all the available packages on the target device. Therefore quite effective classical logic to give a name of the Web domain, recorded on the contrary (as here - ru.alwake), and then - the name of the project. This step is in some way provide a unique name.
Create Activity - the name of the class, which in future will be a subclass of Activity.
Min SDK Version - the minimum version of SDK. If you look at the above list in the Build Target window, we can conclude that our application runs only for devices with Android> = 2.1 (Android 2.1 SDK version corresponds to 7). In this case, it's not very principled, but so be it.

Now you can safely click Finish and contemplate your project in the panel Package Exploper. The project was created and would be nice to get acquainted with some theoretical basis for the Android-device applications, which is well presented here .

About our project

First let's define what we want from our file manager. Ideally - the complete replacement of ASTRO and eStrongs. In the meantime, we need to provide basic navigation through catalogs, and a "base" we mean that we do not need to go into those folders, which has access to only root. In addition, in the above, we will display our current location in directory structure.

Well, first let's talk about the structure of the project:

/ Res/drawable- * dpi - in these three locations we found resources to different extensions of the screen. At the moment there is only the file icon.png, then there is an icon of our application.
/ Res / layout - this folder contains the xml-files that describe the appearance of various forms and form elements. After creating the project there is a file main.xml, there is also need to create a row.xml, which will describe the appearance of each individual series (ie a list item of the tree of our file.)
/ Res / values ​​- here we are any constants that we use in our project.

It should be noted that these XML-files can be edited in visual mode or in text (changing directly xml-code). We will act on the second method. To edit the code, you can right-click on the xml-file in the Package Explorer pane and choose Open with> Text Editor. This is so in the future;)

File - in the file actually contains our main class for the main and only form of application. All of our code in this project will be posted here.
File AndroidManifest.xml - file with the basic properties of our project, particularly given when creating the project (such as, for example, the name). Accordingly, to change, let's name, we need to pick the file.

The remaining files we do not really care.

Start writing code

In general, everything that was greater - the basic information that every programmer must Android-know. And now it's time to deal directly with our code.

File main.xml:
<? xml version ="1.0" encoding ="utf-8" ? >
< TableLayout xmlns:android =""
android:orientation ="vertical"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent" >

< TableRow >
< TextView android:id ="@+id/titleManager"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:padding ="5dip"
</ TableRow >
< TableRow >
< ListView android:id ="@id/android:list"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:layout_weight ="2"
android:drawSelectorOnTop ="false" />
</ TableRow >
</ TableLayout > 

* This source code was highlighted with Source Code Highlighter .

Here we set the markup for our basic layout of the form. TableLayout here means that the elements we have lined up in a table. Then at the top table cell element is placed TextView (text box), and the bottom cell - ListView (list). Both elements have id, using which we can modify the contents of the elements. For example, using for our text field TextView.

File row.xml:
<? xml version ="1.0" encoding ="utf-8" ? >
< TextView
xmlns:android =""
android:layout_width ="fill_parent"
android:layout_height ="40sp"
android:padding ="5dip"
android:gravity ="center_vertical"

* This source code was highlighted with Source Code Highlighter .

Here we set the markup for each item in our ListView, that is, directly to each individual folder or each file. In this code we set the width of each element, height, padding (padding) and alignment center_vertical - ie Center the vertical.

  1. package ru.alwake.filemanager;
  2. import File;
  3. import java.util. ArrayList;
  4. import java.util. List;
  5. import;
  6. import;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.DialogInterface.OnClickListener;
  10. import Uri;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.ListView;
  15. import android.widget.TextView;
  16. public class FileManager extends ListActivity {
  17. private List <String> directoryEntries = new ArrayList <String> ();
  18. private File currentDirectory = new File ("/");
  19. / / When application started
  20. @ Override
  21. public void onCreate (Bundle icicle) {
  22. super.onCreate (icicle);
  23. / / Set main layout
  24. setContentView (R.layout.main);
  25. / / Browse to root directory
  26. browseTo (new File ("/"));
  27. }
  28. / / Browse to parent directory
  29. private void upOneLevel () {
  30. if (this. currentDirectory.getParent ()! = null) {
  31. this. browseTo (this. currentDirectory.getParentFile ());
  32. }
  33. }
  34. / / Browse to file or directory
  35. private void browseTo (final File aDirectory) {
  36. / / If we want to browse directory
  37. if (aDirectory.isDirectory ()) {
  38. / / Fill list with files from this directory
  39. this. currentDirectory = aDirectory;
  40. fill (aDirectory.listFiles ());
  41. / / Set titleManager text
  42. TextView titleManager = (TextView) findViewById (;
  43. titleManager.setText (aDirectory.getAbsolutePath ());
  44. Else {
  45. / / If we want to open file, show this dialog:
  46. / / Listener when YES button clicked
  47. OnClickListener okButtonListener = new OnClickListener () {
  48. public void onClick (DialogInterface arg0, int arg1) {
  49. / / Intent to navigate file
  50. Intent i = new Intent (android.content.Intent.ACTION_VIEW, Uri. Parse ("file: / /" + aDirectory.getAbsolutePath ()));
  51. / / Start this activity
  52. startActivity (i);
  53. }
  54. };
  55. / / Listener when NO button clicked
  56. OnClickListener cancelButtonListener = new OnClickListener () {
  57. public void onClick (DialogInterface arg0, int arg1) {
  58. / / Do nothing
  59. / / Or add something you want
  60. }
  61. };
  62. / / Create dialog
  63. new AlertDialog.Builder (this)
  64. . SetTitle ("Confirmation") / / title
  65. . SetMessage ("Want to open file" + aDirectory.getName () + "?") / / Message
  66. . SetPositiveButton ("Yes", okButtonListener) / / positive button
  67. . SetNegativeButton ("No", cancelButtonListener) / / negative button
  68. Show (); / / show dialog
  69. }
  70. }
  71. / / Fill list
  72. private void fill (File [] files) {
  73. / / Clear list
  74. this. directoryEntries.clear ();
  75. if (this. currentDirectory.getParent ()! = null)
  76. this. directoryEntries.add ("..");
  77. / / Add every file into list
  78. for (File file: files) {
  79. this. directoryEntries.add (file.getAbsolutePath ());
  80. }
  81. / / Create array adapter to show everything
  82. ArrayAdapter <String> directoryList = new ArrayAdapter <String> (this, R.layout.row, this.DirectoryEntries);
  83. this. setListAdapter (directoryList);
  84. }
  85. / / When you clicked onto item
  86. @ Override
  87. protected void onListItemClick (ListView l, View v, int position, long id) {
  88. / / Get selected file name
  89. int selectionRowID = position;
  90. String selectedFileString = this. DirectoryEntries. Get (selectionRowID);
  91. / / If we select ".." then go upper
  92. if (selectedFileString.equals ("..")) {
  93. this. upOneLevel ();
  94. Else {
  95. / / Browse to clicked file or directory using browseTo ()
  96. File clickedFile = null;
  97. clickedFile = new File (selectedFileString);
  98. if (clickedFile! = null)
  99. this. browseTo (clickedFile);
  100. }
  101. }
  102. }
* This source code with WAS Highlighted Source Code Highlighter .

At the beginning of specified package name (package name).
Rows 2-18 are responsible for the import we need libraries. It is important to note that the import libraries, Eclipse can be done automatically, as soon encounters anything unknown.
In this code, we only have five fairly obvious functions, which is easy to understand. And it is - a skeleton application, which provides the main navigation on the file structure. However, in this application there is one problem - when you try to go to the directory to which access is allowed only by root, we get an error and the application quits.
Accordingly, the next time we think about how to get rid of this error and how to display icons corresponding to the type of file. In addition, we will think about implementing the function Copy-Paste in our file manager.

Eventually supposed to get big and clever application that should give basic information about aspects of programming for Android.

The idea and some of the implementation of the file manager is taken from
I would also like to announce that the next part will AndroidDev twentieth of January.