Fixed bug in File browser where Select button gets disabled when browsing into a directory

This commit is contained in:
Ziver Koc 2015-01-15 14:14:00 +01:00
parent 20a120a07e
commit d9c924009d

View file

@ -9,7 +9,9 @@ import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.View;
@ -34,11 +36,11 @@ import java.util.Collections;
import java.util.List;
/** Allow user to select destination directory and to enter filename.
*
/**
* Allow user to select destination directory and to enter filename.
* */
public class FileBrowserDialog extends DialogFragment
implements OnItemClickListener, DialogInterface.OnKeyListener, DialogInterface.OnClickListener{
implements OnItemClickListener, DialogInterface.OnKeyListener, DialogInterface.OnClickListener, TextWatcher {
public static final Logger log = Logger.getLogger(FileBrowserDialog.class);
public static final int FOLDER_ICON = R.drawable.folder;
public static final int FILE_ICON = R.drawable.file;
@ -131,6 +133,7 @@ public class FileBrowserDialog extends DialogFragment
LinearLayout.LayoutParams.WRAP_CONTENT));
fileNameView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
fileNameView.setHint("File Name");
fileNameView.addTextChangedListener(this);
rootView.addView(fileNameView);
}
@ -148,12 +151,13 @@ public class FileBrowserDialog extends DialogFragment
AlertDialog dialog = builder.create();
dialog.show();
if(mode == BrowserMode.SELECT_FILE)
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
return dialog;
}
/**
* A file or folder has been clicked
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
File file = (File)fileListView.getAdapter().getItem(position);
@ -172,6 +176,9 @@ public class FileBrowserDialog extends DialogFragment
}
}
/**
* The "select" button has been pressed
*/
@Override
public void onClick(DialogInterface dialog, int which) {
if(mode == BrowserMode.SELECT_FILE && selection != null)
@ -183,6 +190,9 @@ public class FileBrowserDialog extends DialogFragment
Toast.makeText(this.getActivity(), "No File Selected", Toast.LENGTH_SHORT).show();
}
/**
* Back button has been pressed
*/
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
@ -195,6 +205,23 @@ public class FileBrowserDialog extends DialogFragment
return false;
}
/**
* Text input event for new file text field
*/
@Override
public void afterTextChanged(Editable editable) {
if(this.getDialog() != null) {
if (fileNameView.getText().toString().isEmpty())
((AlertDialog) this.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
else
((AlertDialog) this.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
}
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
private void updateFileList(File dir){
if(!dir.isDirectory())
@ -221,7 +248,9 @@ public class FileBrowserDialog extends DialogFragment
// Reset selection
selection = null;
if(this.getDialog() != null)
if(this.getDialog() != null &&
(mode == BrowserMode.SELECT_FILE ||
mode == BrowserMode.NEW_FILE && fileNameView.getText().toString().isEmpty()))
((AlertDialog)this.getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}