Skip to content

Instantly share code, notes, and snippets.

@SatyaSnehith
Created December 4, 2017 18:37
Show Gist options
  • Save SatyaSnehith/9c01b848231eb2cbcf3075604cd0321b to your computer and use it in GitHub Desktop.
Save SatyaSnehith/9c01b848231eb2cbcf3075604cd0321b to your computer and use it in GitHub Desktop.
File Chooser Dialog for Android
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
/**
* Created by satyasnehith on 3/10/17.
*/
class FileChooserDialog extends Dialog {
private TextView pTextView;
private ListView fListView;
private String path;
private ImageButton imageButton;
private Button cButton, sButton;
private Context context;
private ArrayList<File> fileArrayList;
private ArrayList<File> selectedFiles;
private File[] files;
private File file;
boolean longClick = false;
private FileArrayAdapter adapter;
private FileChooserListener fileChooserListener;
final String CLEAR = "Clear";
private int[] icons = {
R.drawable.black,
R.drawable.blue,
R.drawable.brown,
R.drawable.dblue,
R.drawable.green,
R.drawable.grey,
R.drawable.kgreen,
R.drawable.lblue,
R.drawable.lyellow,
R.drawable.maroon,
R.drawable.red,
R.drawable.vkgreen,
R.drawable.vpink,
R.drawable.yellow,
};
public FileChooserDialog(@NonNull Context context) {
super(context);
this.context = context;
}
public void fileChooserListener(FileChooserListener fileChooserListener){
this.fileChooserListener = fileChooserListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_file_chooser);
path = "/storage/emulated/0/";
pTextView = findViewById(R.id.path);
pTextView.setText(path);
imageButton = findViewById(R.id.ib_back);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
longClick = false;
if (path.equals("/storage/emulated/0/")) {
} else {
clearChoices();
if (path.equals("") || path.equals(null))
dismiss();
String[] split = path.split("/");
path = "";
for (int i = 0; i < split.length - 1; ++i) {
path = path + split[i] + "/";
}
file = new File(path);
pTextView.setText(path);
files = file.listFiles();
fileArrayList.clear();
try {
for (File s : files) {
fileArrayList.add(s);
}
} catch (NullPointerException e) {
e.printStackTrace();
}
updateChecks();
adapter.notifyDataSetChanged();
cButton.setText(CLEAR +" (" +selectedFiles.size() + ")");
}
}
});
cButton = findViewById(R.id.btn_clear);
cButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearChoices();
}
});
sButton = findViewById(R.id.btn_send);
sButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fileChooserListener.onFileChoosen(selectedFiles.toArray(new File[selectedFiles.size()]));
}
});
fileArrayList = new ArrayList<>();
selectedFiles = new ArrayList<>();
file = new File(path);
files = file.listFiles();
for (File s : files) {
fileArrayList.add(s);
}
adapter = new FileArrayAdapter(context);
fListView = findViewById(R.id.files_list);
fListView.setAdapter(adapter);
fListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(selectedFiles.isEmpty())
longClick = false;
if(longClick) {
ImageView imageView = view.findViewById(R.id.iv_check);
if (imageView.getVisibility() == View.VISIBLE) {
imageView.setVisibility(View.INVISIBLE);
selectedFiles.remove(fileArrayList.get(i));
cButton.setText(CLEAR + " (" + selectedFiles.size() + ")");
} else {
imageView.setVisibility(View.VISIBLE);
selectedFiles.add(fileArrayList.get(i));
cButton.setText(CLEAR + " (" + selectedFiles.size() + ")");
}
adapter.getView(i, view, fListView);
} else {
if (path.equals("") || path.equals(null)) {
dismiss();
}
String p = path + fileArrayList.get(i).getName() + "/";
file = new File(p);
if (file.isDirectory()) {
path = p;
pTextView.setText(path);
files = file.listFiles();
fileArrayList.clear();
for (File s : files) {
fileArrayList.add(s);
}
adapter.notifyDataSetChanged();
} else {
ImageView imageView = view.findViewById(R.id.iv_check);
if (imageView.getVisibility() == View.VISIBLE) {
imageView.setVisibility(View.INVISIBLE);
selectedFiles.remove(fileArrayList.get(i));
cButton.setText(CLEAR + " (" + selectedFiles.size() + ")");
} else {
imageView.setVisibility(View.VISIBLE);
selectedFiles.add(fileArrayList.get(i));
cButton.setText(CLEAR + " (" + selectedFiles.size() + ")");
}
adapter.getView(i, view, fListView);
}
}
}
});
fListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClick = true;
ImageView imageView = view.findViewById(R.id.iv_check);
if(imageView.getVisibility() == View.VISIBLE) {
imageView.setVisibility(View.INVISIBLE);
selectedFiles.remove(fileArrayList.get(position));
cButton.setText(CLEAR +" (" +selectedFiles.size() + ")");
} else {
imageView.setVisibility(View.VISIBLE);
selectedFiles.add(fileArrayList.get(position));
cButton.setText(CLEAR +" (" +selectedFiles.size() + ")");
}
adapter.getView(position, view, fListView);
return true;
}
});
}
public void updateChecks() {
}
public void clearChoices() {
selectedFiles.clear();
}
public interface FileChooserListener {
public void onFileChoosen(File[] files);
}
class FileArrayAdapter extends ArrayAdapter<File> {
public FileArrayAdapter(@NonNull Context context) {
super(context, R.layout.file);
}
@Override
public int getCount() {
return fileArrayList.size();
}
@Nullable
@Override
public File getItem(int position) {
return fileArrayList.get(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
View view = convertView;
if(view == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.file, parent, false);
viewHolder.imageView = view.findViewById(R.id.image);
viewHolder.eTextView = view.findViewById(R.id.extension);
viewHolder.nTextView = view.findViewById(R.id.file_name);
viewHolder.tTextView = view.findViewById(R.id.file_type);
viewHolder.cImageView = view.findViewById(R.id.iv_check);
view.setTag(viewHolder);
}
File file = getItem(position);
viewHolder = (ViewHolder) view.getTag();
viewHolder.nTextView.setText(file.getName());
if(file.isDirectory()) {
viewHolder.imageView.setImageResource(R.drawable.dir);
viewHolder.tTextView.setText("Directory");
viewHolder.eTextView.setText("");
} else if(file.isHidden()) {
viewHolder.imageView.setImageResource(R.drawable.black);
viewHolder.tTextView.setText("hidden file");
viewHolder.eTextView.setText("HIDE");
} else if(file.isFile()) {
String extension = getExtension(file.getName()).toUpperCase();
Random random = new Random();
int r = random.nextInt(13) + 1;
viewHolder.imageView.setImageResource(icons[r]);
viewHolder.tTextView.setText(extension + " file");
if(extension.length() < 4)
viewHolder.eTextView.setTextSize(10);
else if(extension.length() < 5)
viewHolder.eTextView.setTextSize(8);
else if(extension.length() < 8)
viewHolder.eTextView.setTextSize(5);
else if (extension.length() > 7) {
extension = extension.substring(0, 7);
viewHolder.eTextView.setTextSize(5);
}
viewHolder.eTextView.setText(extension);
}
if(selectedFiles.contains(file)) {
viewHolder.cImageView.setVisibility(View.VISIBLE);
} else {
viewHolder.cImageView.setVisibility(View.INVISIBLE);
}
return view;
}
private String getExtension(String name) {
String[] split = name.split("\\.");
return split[split.length-1];
}
}
static class ViewHolder {
ImageView imageView;
TextView nTextView, tTextView, eTextView;
ImageView cImageView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment