From c89117c88439eeb36434edd227d39d9a51b8e19d Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Sat, 25 Aug 2018 12:38:16 +0300 Subject: [PATCH] color picker --- .../gr/thmmy/mthmmy/utils/EditorView.java | 51 ++++++++++++++++--- .../res/layout/editor_view_color_picker.xml | 21 ++++++++ app/src/main/res/values/strings.xml | 16 ++++++ app/src/main/res/values/styles.xml | 17 +++++++ 4 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/layout/editor_view_color_picker.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 f4780551..20aba7a7 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java @@ -2,23 +2,25 @@ package gr.thmmy.mthmmy.utils; import android.app.Activity; import android.content.Context; -import android.content.res.Resources; import android.support.v7.widget.AppCompatImageButton; import android.text.Editable; import android.util.AttributeSet; -import android.util.TypedValue; +import android.util.SparseArray; import android.view.LayoutInflater; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; -import android.widget.GridLayout; import android.widget.LinearLayout; +import android.widget.PopupWindow; import gr.thmmy.mthmmy.R; public class EditorView extends LinearLayout { + private SparseArray colors = new SparseArray<>(); + private SparseArray fonts = new SparseArray<>(); + private EditText editText; private AppCompatImageButton emojiButton; private AppCompatImageButton submitButton; @@ -93,12 +95,39 @@ public class EditorView extends LinearLayout { getText().insert(editText.getSelectionStart(), "[s][/s]"); editText.setSelection(editText.getSelectionStart() - 4); }); - // TODO: popup menu for colors + + colors.append(R.id.black, "black"); + colors.append(R.id.red, "red"); + colors.append(R.id.yellow, "yellow"); + colors.append(R.id.pink, "pink"); + colors.append(R.id.green, "green"); + colors.append(R.id.orange, "orange"); + colors.append(R.id.purple, "purple"); + colors.append(R.id.blue, "blue"); + colors.append(R.id.beige, "beige"); + colors.append(R.id.brown, "brown"); + colors.append(R.id.teal, "teal"); + colors.append(R.id.navy, "navy"); + colors.append(R.id.maroon, "maroon"); + colors.append(R.id.lime_green, "limegreen"); + findViewById(R.id.text_color_button).setOnClickListener(view -> { - if (editText.hasSelection()) - editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); - getText().insert(editText.getSelectionStart(), "[color=][/color]"); - editText.setSelection(editText.getSelectionStart() - 8); + PopupWindow popupWindow = new PopupWindow(view.getContext()); + popupWindow.setHeight(LayoutParams.WRAP_CONTENT); + popupWindow.setWidth(LayoutParams.WRAP_CONTENT); + popupWindow.setFocusable(true); + LinearLayout colorPicker = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.editor_view_color_picker, null); + popupWindow.setContentView(colorPicker); + for (int i = 0; i < colorPicker.getChildCount(); i++) { + colorPicker.getChildAt(i).setOnClickListener(v -> { + if (editText.hasSelection()) + editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); + getText().insert(editText.getSelectionStart(), "[color=" + colors.get(v.getId()) + "][/color]"); + editText.setSelection(editText.getSelectionStart() - 8); + popupWindow.dismiss(); + }); + } + popupWindow.showAsDropDown(view); }); findViewById(R.id.text_size_button).setOnClickListener(view -> { if (editText.hasSelection()) @@ -106,6 +135,12 @@ public class EditorView extends LinearLayout { getText().insert(editText.getSelectionStart(), "[size=10pt][/size]"); editText.setSelection(editText.getSelectionStart() - 7); }); + 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[/list]"); + editText.setSelection(editText.getSelectionStart() - 23); + }); findViewById(R.id.align_left_button).setOnClickListener(view -> { if (editText.hasSelection()) editText.getText().delete(editText.getSelectionStart(), editText.getSelectionEnd()); diff --git a/app/src/main/res/layout/editor_view_color_picker.xml b/app/src/main/res/layout/editor_view_color_picker.xml new file mode 100644 index 00000000..7ac8a236 --- /dev/null +++ b/app/src/main/res/layout/editor_view_color_picker.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 879671a4..9167c34c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -156,4 +156,20 @@ Uploading App signature If enabled, an \"uploaded from mTHMMY\" message will be inserted at the end of your uploads descriptions + + + Black + Red + Yellow + Pink + Green + Purple + Orange + Blue + Beige + Brown + Teal + Navy + Maroon + Lime Green diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 4ffe55a1..69d9b065 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -33,6 +33,23 @@ @color/accent + + + + + +