+ // Initialize the database helper. The two `nulls` do not specify the database name or a `CursorFactory`. 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);
+
+ // 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 = 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));
+ }
+ });
+