+ // Get the current folder name.
+ final String currentFolderName = folderCursor.getString(folderCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
+
+ // Display the current folder name in `edit_folder_name_edittext`.
+ final EditText folderNameEditText = alertDialog.findViewById(R.id.edit_folder_name_edittext);
+ folderNameEditText.setText(currentFolderName);
+
+ // Update the status of the edit button when the folder name is changed.
+ 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 newFolderName = s.toString();
+
+ // Get a cursor for the new folder name if it exists.
+ Cursor folderExistsCursor = bookmarksDatabaseHelper.getFolderCursor(newFolderName);
+
+ // Is the new folder name empty?
+ boolean folderNameNotEmpty = !newFolderName.isEmpty();
+
+ // Does the folder name already exist?
+ boolean folderNameAlreadyExists = (!newFolderName.equals(currentFolderName) && (folderExistsCursor.getCount() > 0));
+
+ // Has the folder been renamed?
+ boolean folderRenamed = (!newFolderName.equals(currentFolderName) && !folderNameAlreadyExists);
+
+ // Has the favorite icon changed?
+ boolean iconChanged = (!currentIconRadioButton.isChecked() && !folderNameAlreadyExists);
+
+ // Enable the create button if something has been edited and the new folder name is valid.
+ editButton.setEnabled(folderNameNotEmpty && (folderRenamed || iconChanged));
+ }
+ });
+
+ // Update the status of the edit button when the icon is changed.
+ iconRadioGroup.setOnCheckedChangeListener((RadioGroup group, int checkedId) -> {
+ // Get the new folder name.
+ String newFolderName = folderNameEditText.getText().toString();
+
+ // Get a cursor for the new folder name if it exists.
+ Cursor folderExistsCursor = bookmarksDatabaseHelper.getFolderCursor(newFolderName);
+
+ // Is the new folder name empty?
+ boolean folderNameEmpty = newFolderName.isEmpty();
+
+ // Does the folder name already exist?
+ boolean folderNameAlreadyExists = (!newFolderName.equals(currentFolderName) && (folderExistsCursor.getCount() > 0));
+
+ // Has the folder been renamed?
+ boolean folderRenamed = (!newFolderName.equals(currentFolderName) && !folderNameAlreadyExists);
+
+ // Has the favorite icon changed?
+ boolean iconChanged = (!currentIconRadioButton.isChecked() && !folderNameAlreadyExists);
+
+ // Enable the create button if something has been edited and the new folder name is valid.
+ editButton.setEnabled(!folderNameEmpty && (folderRenamed || iconChanged));
+ });