From e92e2fa75d2de01e9b70e0cb2a9f4a035d2c1cf6 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Sat, 13 Oct 2018 22:25:15 +0300 Subject: [PATCH] allow link insertion without link text --- .../thmmy/mthmmy/editorview/EditorView.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java b/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java index 0cb17141..527b3fcb 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java +++ b/app/src/main/java/gr/thmmy/mthmmy/editorview/EditorView.java @@ -21,6 +21,7 @@ import android.view.LayoutInflater; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; +import android.widget.Button; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.ScrollView; @@ -210,26 +211,33 @@ public class EditorView extends LinearLayout { linkText.getEditText().setText( editText.getText().toString().substring(editText.getSelectionStart(), editText.getSelectionEnd())); } - new AlertDialog.Builder(context, R.style.AppTheme_Dark_Dialog) + AlertDialog linkDialog = new AlertDialog.Builder(context, R.style.AppTheme_Dark_Dialog) .setTitle(R.string.dialog_create_link_title) .setView(dialogBody) - .setPositiveButton(R.string.ok, (dialog, which) -> { - if (TextUtils.isEmpty(Objects.requireNonNull(linkUrl.getEditText()).getText().toString())) { - linkUrl.setError(context.getString(R.string.input_field_required)); - return; - } - if (TextUtils.isEmpty(Objects.requireNonNull(linkText.getEditText()).getText().toString())) { - linkUrl.setError(context.getString(R.string.input_field_required)); - return; - } + .setPositiveButton(R.string.ok, null) + .setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()) + .create(); + linkDialog.setOnShowListener(dialogInterface -> { + Button button = linkDialog.getButton(AlertDialog.BUTTON_POSITIVE); + button.setOnClickListener(view12 -> { + if (TextUtils.isEmpty(Objects.requireNonNull(linkUrl.getEditText()).getText().toString())) { + linkUrl.setError(context.getString(R.string.input_field_required)); + return; + } - if (hadTextSelection) editText.getText().delete(start, end); + if (hadTextSelection) editText.getText().delete(start, end); + if (!TextUtils.isEmpty(linkText.getEditText().getText())) { getText().insert(editText.getSelectionStart(), "[url=" + - Objects.requireNonNull(linkUrl.getEditText()).getText().toString() + "]" + - Objects.requireNonNull(linkText.getEditText()).getText().toString() + "[/url]"); - }) - .setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()) - .show(); + linkUrl.getEditText().getText().toString() + "]" + + linkText.getEditText().getText().toString() + "[/url]"); + } + else + getText().insert(editText.getSelectionStart(), "[url]" + + linkUrl.getEditText().getText().toString() + "[/url]"); + linkDialog.dismiss(); + }); + }); + linkDialog.show(); break; } case R.drawable.ic_format_quote: {