From bcc20f0bafc6c77256080c56ba46054327c77fba Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Wed, 22 Aug 2018 11:56:36 +0300 Subject: [PATCH] bold, italic, underline, strikethrough, text align left, center, right on EditorView --- .../gr/thmmy/mthmmy/utils/EditorView.java | 51 ++++++++++++++++- .../ic_format_align_center_grey_36dp.xml | 5 ++ .../ic_format_align_left_grey_36dp.xml | 5 ++ .../ic_format_align_right_grey_36dp.xml | 5 ++ .../res/drawable/ic_format_bold_grey_36dp.xml | 9 +++ .../drawable/ic_format_italic_grey_36dp.xml | 4 ++ .../ic_format_list_bulleted_black_36dp.xml | 5 ++ .../ic_format_underlined_grey_36dp.xml | 5 ++ .../drawable/ic_strikethrough_s_grey_36dp.xml | 5 ++ app/src/main/res/layout/editor_view.xml | 57 ++++++++++++++++++- 10 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/drawable/ic_format_align_center_grey_36dp.xml create mode 100644 app/src/main/res/drawable/ic_format_align_left_grey_36dp.xml create mode 100644 app/src/main/res/drawable/ic_format_align_right_grey_36dp.xml create mode 100644 app/src/main/res/drawable/ic_format_bold_grey_36dp.xml create mode 100644 app/src/main/res/drawable/ic_format_italic_grey_36dp.xml create mode 100644 app/src/main/res/drawable/ic_format_list_bulleted_black_36dp.xml create mode 100644 app/src/main/res/drawable/ic_format_underlined_grey_36dp.xml create mode 100644 app/src/main/res/drawable/ic_strikethrough_s_grey_36dp.xml diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java b/app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java index 2eee42d3..48f2c543 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java @@ -17,7 +17,7 @@ import gr.thmmy.mthmmy.R; public class EditorView extends LinearLayout { private EditText editText; - AppCompatImageButton emojiButton; + private AppCompatImageButton emojiButton; private AppCompatImageButton submitButton; private EmojiKeyboard.EmojiKeyboardOwner emojiKeyboardOwner; private boolean emojiKeyboardVisible = false; @@ -39,6 +39,7 @@ public class EditorView extends LinearLayout { private void init(Context context, AttributeSet attrs) { LayoutInflater.from(context).inflate(R.layout.editor_view, this, true); + setOrientation(VERTICAL); editText = findViewById(R.id.editor_edittext); emojiButton = findViewById(R.id.emoji_keyboard_button); @@ -65,6 +66,54 @@ public class EditorView extends LinearLayout { }); submitButton = findViewById(R.id.submit_button); + findViewById(R.id.bold_button).setOnClickListener(view -> { + if (editText.hasSelection()) + editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); + getText().insert(editText.getSelectionStart(), "[b][/b]"); + editText.setSelection(editText.getSelectionStart() - 4); + }); + findViewById(R.id.italic_button).setOnClickListener(view -> { + if (editText.hasSelection()) + editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); + getText().insert(editText.getSelectionStart(), "[i][/i]"); + editText.setSelection(editText.getSelectionStart() - 4); + }); + findViewById(R.id.underline_button).setOnClickListener(view -> { + if (editText.hasSelection()) + editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); + getText().insert(editText.getSelectionStart(), "[u][/u]"); + editText.setSelection(editText.getSelectionStart() - 4); + }); + findViewById(R.id.strikethrough_button).setOnClickListener(view -> { + if (editText.hasSelection()) + editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); + getText().insert(editText.getSelectionStart(), "[s][/s]"); + editText.setSelection(editText.getSelectionStart() - 4); + }); + findViewById(R.id.unordered_list_button).setOnClickListener(view -> { + if (editText.hasSelection()) + editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); + getText().insert(editText.getSelectionStart(), "[list]\n[li][/li]\n[li][/li]\n[li][/li]\n[/list]"); + editText.setSelection(editText.getSelectionStart() - 33); + }); + findViewById(R.id.align_left_button).setOnClickListener(view -> { + if (editText.hasSelection()) + editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); + getText().insert(editText.getSelectionStart(), "[left][/left]"); + editText.setSelection(editText.getSelectionStart() - 7); + }); + findViewById(R.id.align_center_button).setOnClickListener(view -> { + if (editText.hasSelection()) + editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); + getText().insert(editText.getSelectionStart(), "[center][/center]"); + editText.setSelection(editText.getSelectionStart() - 9); + }); + findViewById(R.id.align_right_button).setOnClickListener(view -> { + if (editText.hasSelection()) + editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); + getText().insert(editText.getSelectionStart(), "[right][/right]"); + editText.setSelection(editText.getSelectionStart() - 8); + }); } public Editable getText() { diff --git a/app/src/main/res/drawable/ic_format_align_center_grey_36dp.xml b/app/src/main/res/drawable/ic_format_align_center_grey_36dp.xml new file mode 100644 index 00000000..001540aa --- /dev/null +++ b/app/src/main/res/drawable/ic_format_align_center_grey_36dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_format_align_left_grey_36dp.xml b/app/src/main/res/drawable/ic_format_align_left_grey_36dp.xml new file mode 100644 index 00000000..c0688aef --- /dev/null +++ b/app/src/main/res/drawable/ic_format_align_left_grey_36dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_format_align_right_grey_36dp.xml b/app/src/main/res/drawable/ic_format_align_right_grey_36dp.xml new file mode 100644 index 00000000..f486de4a --- /dev/null +++ b/app/src/main/res/drawable/ic_format_align_right_grey_36dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_format_bold_grey_36dp.xml b/app/src/main/res/drawable/ic_format_bold_grey_36dp.xml new file mode 100644 index 00000000..2a0ef3cf --- /dev/null +++ b/app/src/main/res/drawable/ic_format_bold_grey_36dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_format_italic_grey_36dp.xml b/app/src/main/res/drawable/ic_format_italic_grey_36dp.xml new file mode 100644 index 00000000..31ae9249 --- /dev/null +++ b/app/src/main/res/drawable/ic_format_italic_grey_36dp.xml @@ -0,0 +1,4 @@ + + + diff --git a/app/src/main/res/drawable/ic_format_list_bulleted_black_36dp.xml b/app/src/main/res/drawable/ic_format_list_bulleted_black_36dp.xml new file mode 100644 index 00000000..228e7a79 --- /dev/null +++ b/app/src/main/res/drawable/ic_format_list_bulleted_black_36dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_format_underlined_grey_36dp.xml b/app/src/main/res/drawable/ic_format_underlined_grey_36dp.xml new file mode 100644 index 00000000..68fa161a --- /dev/null +++ b/app/src/main/res/drawable/ic_format_underlined_grey_36dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_strikethrough_s_grey_36dp.xml b/app/src/main/res/drawable/ic_strikethrough_s_grey_36dp.xml new file mode 100644 index 00000000..9ee72210 --- /dev/null +++ b/app/src/main/res/drawable/ic_strikethrough_s_grey_36dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/editor_view.xml b/app/src/main/res/layout/editor_view.xml index 3863f711..9387c8cc 100644 --- a/app/src/main/res/layout/editor_view.xml +++ b/app/src/main/res/layout/editor_view.xml @@ -5,10 +5,65 @@ android:layout_height="wrap_content"> + + + + + + + + + + + + + + +