- // Allow the `enter` key on the keyboard to create the folder from `create_folder_name_edittext`.
- EditText createFolderNameEditText = (EditText) alertDialog.findViewById(R.id.create_folder_name_edittext);
- createFolderNameEditText.setOnKeyListener(new View.OnKeyListener() {
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
- if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
- // Trigger `createBookmarkFolderListener` and return the `DialogFragment` to the parent activity.
- createBookmarkFolderListener.onCreateBookmarkFolder(CreateBookmarkFolderDialog.this);
- // Manually dismiss the `AlertDialog`.
- alertDialog.dismiss();
- // Consume the event.
- return true;
- } else { // If any other key was pressed do not consume the event.
- return false;
- }
+ // Get handles for the views in the dialog.
+ final Button createButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
+ EditText folderNameEditText = alertDialog.findViewById(R.id.create_folder_name_edittext);
+ ImageView webPageIconImageView = alertDialog.findViewById(R.id.create_folder_web_page_icon);
+
+ // Initially disable the create button.
+ createButton.setEnabled(false);
+
+ // Initialize the database helper. The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
+ final BookmarksDatabaseHelper bookmarksDatabaseHelper = new BookmarksDatabaseHelper(getContext(), null, null, 0);
+
+ // Enable the create button if the new folder name is unique.
+ folderNameEditText.addTextChangedListener(new TextWatcher() {
+ @Override
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+ // Do nothing.
+ }
+
+ @Override
+ public void onTextChanged(CharSequence s, int start, int before, int count) {
+ // Do nothing.
+ }
+
+ @Override
+ public void afterTextChanged(Editable s) {
+ // Convert the current text to a string.
+ String folderName = s.toString();
+
+ // Check if a folder with the name already exists.
+ Cursor folderExistsCursor = bookmarksDatabaseHelper.getFolder(folderName);
+
+ // Enable the create button if the new folder name is not empty and doesn't already exist.
+ createButton.setEnabled(!folderName.isEmpty() && (folderExistsCursor.getCount() == 0));
+ }
+ });
+
+ // Allow the enter key on the keyboard to create the folder from `create_folder_name_edittext`.
+ folderNameEditText.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
+ // If the event is a key-down on the `enter` key, select the `PositiveButton` `Create`.
+ if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER) && createButton.isEnabled()) { // The enter key was pressed and the create button is enabled.
+ // Trigger `createBookmarkFolderListener` and return the `DialogFragment` to the parent activity.
+ createBookmarkFolderListener.onCreateBookmarkFolder(this, favoriteIconBitmap);
+ // Manually dismiss the `AlertDialog`.
+ alertDialog.dismiss();
+ // Consume the event.
+ return true;
+ } else { // If any other key was pressed, or if the create button is currently disabled, do not consume the event.
+ return false;