+ // Get handles for the views.
+ shortcutNameEditText = alertDialog.findViewById(R.id.shortcut_name_edittext);
+ urlEditText = alertDialog.findViewById(R.id.url_edittext);
+ openWithPrivacyBrowserRadioButton = alertDialog.findViewById(R.id.open_with_privacy_browser_radiobutton);
+ createButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
+
+ // Populate the edit texts.
+ shortcutNameEditText.setText(initialShortcutName);
+ urlEditText.setText(initialUrlString);
+
+ // Add a text change listener to the shortcut name edit text.
+ shortcutNameEditText.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) {
+ // Update the create button.
+ updateCreateButton();
+ }
+ });
+
+ // Add a text change listener to the URL edit text.
+ urlEditText.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) {
+ // Update the create button.
+ updateCreateButton();
+ }
+ });
+
+ // Allow the enter key on the keyboard to create the shortcut when editing the name.
+ shortcutNameEditText.setOnKeyListener((View view, int keyCode, KeyEvent keyEvent) -> {
+ // Check to see if the enter key was pressed.
+ if ((keyEvent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
+ // Check the status of the create button.
+ if (createButton.isEnabled()) { // The create button is enabled.
+ // Create the home screen shortcut.
+ createHomeScreenShortcut();