Pages in Example
Standalone fragment
To use the fragment together with an existing toolbar/action bar, a few things should be overridden. Here’s a minimal example where the toolbar is intercepted from being set as the main toolbar. The menu creation is also intercepted and populates the toolbar directly. public class StandaloneFilePickerFragment extends FilePickerFragment { protected Toolbar mToolbar; @Override protected void setupToolbar(Toolbar toolbar) { // Prevent it from being set as main toolbar by NOT calling super.setupToolbar().
Override selection behavior
New in 2.4.0 are overridable methods to handle UI-interactions. The following methods are now available for augmentation: onClickOK, handles ok button. onClickCancel, handles cancel button. onClickHeader, handles clicks on “..”. onClickDir, handles clicks on non-selectable items (usually directories). onLongClickDir, handles long clicks on non-selectable items. onClickCheckable, handles clicks on selectable items. onLongClickCheckable, handles long clicks on selectable items. onClickCheckBox, handles clicks on the checkbox of selectable items. Please see the existing implementations before you override any of them.
Override the back button
In case you want the back button to navigate the hierarchy instead of instantly exiting the activity, this is one approach you might take. Create an activity which overrides the back button and loads a custom fragment package com.nononsenseapps.filepicker.examples.backbutton; import android.os.Environment; import com.nononsenseapps.filepicker.AbstractFilePickerFragment; import com.nononsenseapps.filepicker.FilePickerActivity; import java.io.File; public class BackHandlingFilePickerActivity extends FilePickerActivity { /** * Need access to the fragment */ BackHandlingFilePickerFragment currentFragment; /** * Return a copy of the new fragment and set the variable above.
Filter based on file extension
By default, the SD-card picker will display all files in alphabetical order. But let’s say that your app can only handle a specific type of file, like .txt-files. Here’s a minimal example which will only display such files. First, a convenience method to get the extension of files: // File extension to filter on, including the initial dot. private static final String EXTENSION = ".txt"; /** * * @param file * @return The file extension.
Custom item layout
Say you want to browse some files which have really long names. By default, filenames will be cut if they exceed one line in width like ThisIsAReallyLongFi…. What if we really wanted it show like in this image? The behavior of the text is defined in the listitem layouts: nnf_filepicker_listitem_checkable and nnf_filepicker_listitem_dir. There are two kinds of layouts, one with a checkbox to allow selection, and one without a checkbox. The second one is also used for the special header item ..
Change the sort order
By default, the SD-card picker will display all files in alphabetical order. But what if you want a different sort-order? You can override the sorting by overriding the compareFiles-method: @Override protected int compareFiles(File lhs, File rhs) { if (lhs.isDirectory() && !rhs.isDirectory()) { return -1; } else if (rhs.isDirectory() && !lhs.isDirectory()) { return 1; } // This was the previous behaviour for all file-file comparisons. Now it's // only done if the files have the same extension, or no extension.