Created
September 13, 2018 20:02
-
-
Save Binary-Finery/dd1db4b8ff74d798fda934decfb74be8 to your computer and use it in GitHub Desktop.
explorer v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.DialogInterface; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.EditText; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class FileExplorer extends AppCompatActivity { | |
private TextView tvPath; | |
private ListView listView; | |
private List<FileData> allFiles, dirFiles; | |
private static String currentDirectory = ""; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_file_explorer); | |
Toolbar toolbar = findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
initViews(); | |
currentDirectory = Environment.getExternalStorageDirectory().toString(); | |
dirFiles = new ArrayList<>(); | |
allFiles = getDirFiles(); | |
populateListView(); | |
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { | |
String fName = allFiles.get(i).getfName(); | |
File file = new File(currentDirectory, fName); | |
if (file.isFile()) { | |
Toast.makeText(getApplicationContext(), "this is a txt file", Toast.LENGTH_LONG).show(); | |
} else { | |
currentDirectory = file.toString(); | |
populateListView(); | |
} | |
} | |
}); | |
} | |
private List<FileData> getDirFiles() { | |
dirFiles.clear(); | |
File dir = new File(currentDirectory); | |
for (File file : dir.listFiles()) { | |
if ((file.isFile() && file.getName().endsWith(".txt")) || file.isDirectory()) { | |
dirFiles.add(new FileData(file.getName(), file.isDirectory())); | |
} | |
} | |
tvPath.setText(dir.toString()); | |
return dirFiles; | |
} | |
private void populateListView() { | |
allFiles = getDirFiles(); | |
listView.setAdapter(new FileAdapter(FileExplorer.this, allFiles)); | |
} | |
public void clickEvent(View view) { | |
switch (view.getId()) { | |
case R.id.iv_new_folder: | |
createNewFolderDialog(); | |
break; | |
} | |
} | |
private void createNewFolderDialog() { | |
final AlertDialog builder = new AlertDialog.Builder(FileExplorer.this).create(); | |
View v = LayoutInflater.from(this).inflate(R.layout.new_folder_dialog, null); | |
final EditText folderName = v.findViewById(R.id.et_new_folder_name); | |
builder.setView(v); | |
builder.setTitle("New Folder"); | |
builder.setButton(DialogInterface.BUTTON_POSITIVE, "create", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
String name = folderName.getText().toString().trim(); | |
if (name.length() > 0) { | |
createFolder(name); | |
} | |
} | |
}); | |
builder.setButton(DialogInterface.BUTTON_NEGATIVE, "cancel", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialogInterface, int i) { | |
builder.dismiss(); | |
} | |
}); | |
builder.show(); | |
} | |
private void createFolder(String folderName) { | |
boolean created = false; | |
File dir = new File(currentDirectory, folderName); | |
if (!dir.exists()) { | |
created = dir.mkdirs(); | |
} else { | |
msg("folder already exists!"); | |
} | |
if (!created) { | |
msg("unable to create new folder"); | |
} else { | |
populateListView(); | |
msg("new folder created"); | |
} | |
} | |
private void initViews() { | |
listView = findViewById(R.id.lv_files); | |
tvPath = findViewById(R.id.tv_path); | |
} | |
private void msg(String s) { | |
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onBackPressed() { | |
if (!currentDirectory.equals(Environment.getExternalStorageDirectory().toString())) { | |
File c = new File(currentDirectory); | |
currentDirectory = c.getParent(); | |
populateListView(); | |
} else { | |
super.onBackPressed(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment