Back to project page material-dialogs.
The source code is released under:
MIT License
If you think the Android project material-dialogs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.afollestad.materialdialogssample; //from w ww . j a va2 s . c o m import android.app.Activity; import android.app.Dialog; import android.app.DialogFragment; import android.os.Bundle; import android.os.Environment; import android.view.View; import com.afollestad.materialdialogs.MaterialDialog; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * @author Aidan Follestad (afollestad) */ public class FolderSelectorDialog extends DialogFragment implements MaterialDialog.ListCallback { private File parentFolder; private File[] parentContents; private boolean canGoUp = true; private FolderSelectCallback mCallback; private final MaterialDialog.ButtonCallback mButtonCallback = new MaterialDialog.ButtonCallback() { @Override public void onPositive(MaterialDialog materialDialog) { materialDialog.dismiss(); mCallback.onFolderSelection(parentFolder); } @Override public void onNegative(MaterialDialog materialDialog) { materialDialog.dismiss(); } }; public static interface FolderSelectCallback { void onFolderSelection(File folder); } public FolderSelectorDialog() { parentFolder = Environment.getExternalStorageDirectory(); parentContents = listFiles(); } String[] getContentsArray() { String[] results = new String[parentContents.length + (canGoUp ? 1 : 0)]; if (canGoUp) results[0] = "..."; for (int i = 0; i < parentContents.length; i++) results[canGoUp ? i + 1 : i] = parentContents[i].getName(); return results; } File[] listFiles() { File[] contents = parentFolder.listFiles(); List<File> results = new ArrayList<>(); for (File fi : contents) { if (fi.isDirectory()) results.add(fi); } Collections.sort(results, new FolderSorter()); return results.toArray(new File[results.size()]); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return new MaterialDialog.Builder(getActivity()) .title(parentFolder.getAbsolutePath()) .items(getContentsArray()) .itemsCallback(this) .callback(mButtonCallback) .autoDismiss(false) .positiveText(R.string.choose) .negativeText(android.R.string.cancel) .build(); } @Override public void onSelection(MaterialDialog materialDialog, View view, int i, CharSequence s) { if (canGoUp && i == 0) { parentFolder = parentFolder.getParentFile(); canGoUp = parentFolder.getParent() != null; } else { parentFolder = parentContents[canGoUp ? i - 1 : i]; canGoUp = true; } parentContents = listFiles(); MaterialDialog dialog = (MaterialDialog) getDialog(); dialog.setTitle(parentFolder.getAbsolutePath()); dialog.setItems(getContentsArray()); } @Override public void onAttach(Activity activity) { super.onAttach(activity); mCallback = (FolderSelectCallback) activity; } public void show(Activity context) { show(context.getFragmentManager(), "FOLDER_SELECTOR"); } private static class FolderSorter implements Comparator<File> { @Override public int compare(File lhs, File rhs) { return lhs.getName().compareTo(rhs.getName()); } } }