Make folder picker text view editable

This gives users the ability to input any path directly without having to walk the whole directory tree (which may include non-readable files)
This commit is contained in:
Adrian Ulrich 2017-10-21 19:45:17 +02:00
parent 6af743d4dd
commit feed3100be
2 changed files with 55 additions and 12 deletions

View File

@ -25,14 +25,16 @@ THE SOFTWARE.
xmlns:dslv="http://schemas.android.com/apk/res-auto" xmlns:dslv="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" > android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true" >
<LinearLayout <LinearLayout
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" > android:orientation="horizontal" >
<TextView <EditText
android:id="@+id/path_display" android:id="@+id/path_display"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_width="0dp" android:layout_width="0dp"

View File

@ -23,17 +23,20 @@ import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View; import android.view.View;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.Menu; import android.view.Menu;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.Button; import android.widget.Button;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
import com.mobeta.android.dslv.DragSortListView; import com.mobeta.android.dslv.DragSortListView;
public abstract class FolderPickerActivity extends Activity public abstract class FolderPickerActivity extends Activity
implements AdapterView.OnItemClickListener, implements AdapterView.OnItemClickListener,
AdapterView.OnItemLongClickListener AdapterView.OnItemLongClickListener
@ -46,7 +49,7 @@ public abstract class FolderPickerActivity extends Activity
/** /**
* View displaying the current path * View displaying the current path
*/ */
private TextView mPathDisplay; private EditText mPathDisplay;
/** /**
* Save button * Save button
*/ */
@ -70,21 +73,48 @@ public abstract class FolderPickerActivity extends Activity
mListAdapter = new FolderPickerAdapter(this, 0); mListAdapter = new FolderPickerAdapter(this, 0);
mListView = (DragSortListView)findViewById(R.id.list); mListView = (DragSortListView)findViewById(R.id.list);
mPathDisplay = (TextView) findViewById(R.id.path_display); mPathDisplay = (EditText) findViewById(R.id.path_display);
mSaveButton = (Button) findViewById(R.id.save_button); mSaveButton = (Button) findViewById(R.id.save_button);
mListView.setAdapter(mListAdapter); mListView.setAdapter(mListAdapter);
mListView.setOnItemClickListener(this); mListView.setOnItemClickListener(this);
mSaveButton.setOnClickListener(new View.OnClickListener() { mPathDisplay.addTextChangedListener(mTextWatcher);
mSaveButton.setOnClickListener(mSaveButtonClickListener);
// init defaults
enableTritasticSelect(false, null, null);
}
/**
* Callback for Save button click events
*/
private final View.OnClickListener mSaveButtonClickListener = new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
onFolderPicked(mListAdapter.getCurrentDir(), onFolderPicked(mListAdapter.getCurrentDir(),
mListAdapter.getIncludedDirs(), mListAdapter.getIncludedDirs(),
mListAdapter.getExcludedDirs()); mListAdapter.getExcludedDirs());
}});
// init defaults
enableTritasticSelect(false, null, null);
} }
};
/**
* Callback for EditText change events
*/
private final TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
final File dir = new File(s.toString());
setCurrentDir(dir);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
/** /**
* Called after a folder was selected * Called after a folder was selected
@ -120,9 +150,20 @@ public abstract class FolderPickerActivity extends Activity
* @param dir the directory to jump to * @param dir the directory to jump to
*/ */
void setCurrentDir(File dir) { void setCurrentDir(File dir) {
mPathDisplay.setText(dir.getAbsolutePath()); mSaveButton.setEnabled(dir.isDirectory());
mListAdapter.setCurrentDir(dir); mListAdapter.setCurrentDir(dir);
mListView.setSelectionFromTop(0, 0); mListView.setSelectionFromTop(0, 0);
final File labelDir = new File(mPathDisplay.getText().toString());
if (!dir.equals(labelDir)) {
// Only update the text field if the actual dir doesn't equal
// the currently displayed path, even if the text isn't exactly
// the same. We do this to avoid 'jumps' where we would replace
// '/storage/x/' with '/storage/x'.
final String label = dir.getAbsolutePath();
mPathDisplay.setText(label);
mPathDisplay.setSelection(label.length());
}
} }
/** /**