+ // Get a handle for the create button.
+ final Button createButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
+ EditText folderNameEditText = (EditText) alertDialog.findViewById(R.id.create_folder_name_edittext);
+
+ // Initially disable the create button.
+ createButton.setEnabled(false);
+
+ // 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 = BookmarksActivity.bookmarksDatabaseHelper.getFolderCursor(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));
+ }
+ });
+