HERE FIRST IS JAVE CODE FOR MAKING A FILE MANAGER ON ANDROID STUDIO BY USING COPE PASTE CODE.
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private File currentDir;
private FileArrayAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currentDir = new File("/sdcard/");
fill(currentDir);
}
private void fill(File f) {
File[]dirs = f.listFiles();
this.setTitle("Current Dir: "+f.getName());
List<Option>dir = new ArrayList<Option>();
List<Option>fls = new ArrayList<Option>();
try{
for(File ff: dirs) {
if(ff.isDirectory())
dir.add(new Option(ff.getName(),"Folder",ff.getAbsolutePath()));
else {
fls.add(new Option(ff.getName(),"File Size: "+ff.length(),ff.getAbsolutePath()));
}
}
}catch(Exception e) {
}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if(!f.getName().equalsIgnoreCase("sdcard"))
dir.add(0,new Option("..","Parent Directory",f.getParent()));
adapter = new FileArrayAdapter(MainActivity.this,R.layout.file_view,dir);
this.fileList.setAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Option o = adapter.getItem(position);
if(o.getData().equalsIgnoreCase("folder")||o.getData().equalsIgnoreCase("parent directory")) {
currentDir = new File(o.getPath());
fill(currentDir);
}
else {
onFileClick(o);
}
}
private void onFileClick(Option o) {
Toast.makeText(this, "File Clicked: "+o.getName(), Toast.LENGTH_SHORT).show();
}
}
HERE IS THE XML CODE OF ANDROID STUDIO BY MAKING A FILE MANAGER ON ANDROID STUDIO.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/file_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no_files"
android:textAlignment="center"
android:visibility="gone" />
</LinearLayout>
This layout file contains a ListView
element to display a list of files and a TextView
element to display a message when there are no files to show.
Here is an example of the corresponding Java code to populate the ListView
with a list of files:
ListView listView = findViewById(R.id.file_list);
File[] files = getFiles();
ArrayAdapter<File> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, files);
listView.setAdapter(adapter);
This code gets a list of File
objects, creates an ArrayAdapter
with these files, and sets the adapter on the ListView
. The ArrayAdapter
will handle populating the ListView
with the file names.
PASTE THIS CODE AND MAKE YOUR OWN APP.
0 Comments