diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/TestEditView.java b/app/src/main/java/gr/thmmy/mthmmy/activities/TestEditView.java index 395e937a..8ea9903f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/TestEditView.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/TestEditView.java @@ -2,27 +2,43 @@ package gr.thmmy.mthmmy.activities; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; -import android.text.InputType; -import android.view.inputmethod.EditorInfo; +import android.view.View; import android.view.inputmethod.InputConnection; -import android.widget.EditText; import gr.thmmy.mthmmy.R; +import gr.thmmy.mthmmy.utils.EditorView; import gr.thmmy.mthmmy.utils.EmojiKeyboard; -public class TestEditView extends AppCompatActivity { +public class TestEditView extends AppCompatActivity implements EmojiKeyboard.EmojiKeyboardOwner { + + EmojiKeyboard emojiKeyboard; + EditorView editorView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_edit_view); - EditText testEdittext = (EditText) findViewById(R.id.test_edittext); - EmojiKeyboard emojiKeyboard = (EmojiKeyboard) findViewById(R.id.emoji_keyboard); + editorView = (EditorView) findViewById(R.id.editor_view); + emojiKeyboard = (EmojiKeyboard) findViewById(R.id.emoji_keyboard); - testEdittext.setRawInputType(InputType.TYPE_CLASS_TEXT); - testEdittext.setTextIsSelectable(true); - InputConnection ic = testEdittext.onCreateInputConnection(new EditorInfo()); + InputConnection ic = editorView.getInputConnection(); emojiKeyboard.setInputConnection(ic); + editorView.setEmojiKeyboardOwner(this); + } + + @Override + public void setEmojiKeyboardVisible(boolean visible) { + emojiKeyboard.setVisibility(visible ? View.VISIBLE : View.GONE); + } + + @Override + public void onBackPressed() { + if (emojiKeyboard.getVisibility() == View.VISIBLE) { + emojiKeyboard.setVisibility(View.GONE); + editorView.notifyKeyboardVisibility(false); + } else { + super.onBackPressed(); + } } } 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 16f1847d..94feea0a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java @@ -1,39 +1,67 @@ package gr.thmmy.mthmmy.utils; +import android.app.Activity; import android.content.Context; -import android.inputmethodservice.Keyboard; -import android.inputmethodservice.KeyboardView; -import android.support.design.widget.TextInputLayout; import android.support.v7.widget.AppCompatImageButton; import android.text.Editable; +import android.util.AttributeSet; 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.LinearLayout; -import android.widget.RelativeLayout; import gr.thmmy.mthmmy.R; -public class EditorView extends RelativeLayout implements KeyboardView.OnKeyboardActionListener { - - public final static int SMILE = 10; +public class EditorView extends LinearLayout { private EditText editText; + AppCompatImageButton emojiButton; private AppCompatImageButton submitButton; + private EmojiKeyboard.EmojiKeyboardOwner emojiKeyboardOwner; + private boolean emojiKeyboardVisible = false; public EditorView(Context context) { super(context); + init(context, null); + } + + public EditorView(Context context, AttributeSet attrs) { + super(context, attrs); + init(context, attrs); + } + + public EditorView(Context context, AttributeSet attrs, int defStyleAttrs) { + super(context, attrs, defStyleAttrs); + init(context, attrs); + } - LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - inflater.inflate(R.layout.editor_view, this, true); + private void init(Context context, AttributeSet attrs) { + setOrientation(VERTICAL); + LayoutInflater.from(context).inflate(R.layout.editor_view, this, true); editText = (EditText) findViewById(R.id.editor_edittext); + emojiButton = (AppCompatImageButton) findViewById(R.id.emoji_keyboard_button); + editText.setOnTouchListener((v, event) -> { + if (emojiKeyboardVisible) return true; + return false; + }); + emojiButton.setOnClickListener(view -> { + InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE); + if (emojiKeyboardVisible) { + editText.requestFocus(); + imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); + emojiButton.setImageResource(R.drawable.ic_tag_faces_grey_24dp); + } else { + imm.hideSoftInputFromWindow(getWindowToken(), 0); + view.clearFocus(); + emojiButton.setImageResource(R.drawable.ic_keyboard_grey_24dp); + } + emojiKeyboardVisible = !emojiKeyboardVisible; + emojiKeyboardOwner.setEmojiKeyboardVisible(emojiKeyboardVisible); + }); submitButton = (AppCompatImageButton) findViewById(R.id.submit_button); - - /*Keyboard emojiKeyboard = new Keyboard(context, R.xml.emoji_keyboard); - KeyboardView emojiKeyboardView = (KeyboardView) getChildAt(2); - emojiKeyboardView.setKeyboard(emojiKeyboard); - emojiKeyboardView.setPreviewEnabled(false); - emojiKeyboardView.setOnKeyboardActionListener(this);*/ } public Editable getText() { @@ -48,53 +76,21 @@ public class EditorView extends RelativeLayout implements KeyboardView.OnKeyboar submitButton.setOnClickListener(onSubmitListener); } - @Override - public void onPress(int primaryCode) { - + public void setEmojiKeyboardOwner(EmojiKeyboard.EmojiKeyboardOwner emojiKeyboardOwner) { + this.emojiKeyboardOwner = emojiKeyboardOwner; } - @Override - public void onRelease(int primaryCode) { - + public InputConnection getInputConnection() { + return editText.onCreateInputConnection(new EditorInfo()); } - @Override - public void onKey(int primaryCode, int[] keyCodes) { - Editable editable = editText.getText(); - if (editText.hasSelection()) - editable.delete(editText.getSelectionStart(), editText.getSelectionEnd()); - int cursorIndex = editText.getSelectionStart(); - String appendedText = ""; - switch (primaryCode) { - case SMILE: - appendedText = "^:)^"; - break; + public void notifyKeyboardVisibility(boolean visible) { + if (visible) { + emojiButton.setImageResource(R.drawable.ic_keyboard_grey_24dp); + emojiKeyboardVisible = true; + } else { + emojiButton.setImageResource(R.drawable.ic_tag_faces_grey_24dp); + emojiKeyboardVisible = false; } - editable.insert(cursorIndex, appendedText); - } - - @Override - public void onText(CharSequence text) { - - } - - @Override - public void swipeLeft() { - - } - - @Override - public void swipeRight() { - - } - - @Override - public void swipeDown() { - - } - - @Override - public void swipeUp() { - } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java b/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java index 2c15979d..3a83ca5f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java @@ -1,6 +1,8 @@ package gr.thmmy.mthmmy.utils; import android.content.Context; +import android.support.v7.widget.AppCompatImageButton; +import android.text.TextUtils; import android.util.AttributeSet; import android.util.SparseArray; import android.view.LayoutInflater; @@ -35,7 +37,7 @@ public class EmojiKeyboard extends LinearLayout { // add space before emoji emojis.append(R.drawable.heart, "<3"); - // this was copied twice in the original page for some reason + // this was twice in the original page for some reason, with another alias "locked" emojis.append(R.drawable.locked, "^lock^"); emojis.append(R.drawable.roll_over, "^rollover^"); emojis.append(R.drawable.redface, "^redface^"); @@ -48,7 +50,6 @@ public class EmojiKeyboard extends LinearLayout { emojis.append(R.drawable.mad, "^mad^"); emojis.append(R.drawable.wav, "^wav^"); emojis.append(R.drawable.binkybaby, "^binkybaby^"); - // maybe renamed emojis.append(R.drawable.police, "^police^"); emojis.append(R.drawable.dontknow, "^dontknow^"); // the next two are the same thing? @@ -56,7 +57,6 @@ public class EmojiKeyboard extends LinearLayout { emojis.append(R.drawable.angry_hot, "^angryhot^"); emojis.append(R.drawable.angry, "^angry^"); emojis.append(R.drawable.foyska, "^fouska^"); - // changed icon name to become valid drawable name emojis.append(R.drawable.e10_7_3e, "^sfinaki^"); emojis.append(R.drawable.bang_head, "^banghead^"); emojis.append(R.drawable.crybaby, "^crybaby^"); @@ -64,7 +64,6 @@ public class EmojiKeyboard extends LinearLayout { emojis.append(R.drawable.jerk, "^jerk^"); emojis.append(R.drawable.nono, "^nono^"); emojis.append(R.drawable.notworthy, "^notworthy^"); - // changed icon name to become valid drawable name emojis.append(R.drawable.off_topic, "^off-topic^"); emojis.append(R.drawable.puke, "^puke^"); emojis.append(R.drawable.shout, "^shout^"); @@ -108,7 +107,6 @@ public class EmojiKeyboard extends LinearLayout { emojis.append(R.drawable.seestars, "^seestars^"); emojis.append(R.drawable.sfyri, "^sfyri^"); emojis.append(R.drawable.spam2, "^spam^"); - // changed icon name to become valid drawable name emojis.append(R.drawable.esuper, "^super^"); emojis.append(R.drawable.tafos, "^tafos^"); emojis.append(R.drawable.tomatomourh, "^tomato^"); @@ -139,13 +137,20 @@ public class EmojiKeyboard extends LinearLayout { emojis.append(R.drawable.smurf, "^smurf^"); GridView emojiGridView = (GridView) findViewById(R.id.emoji_gridview); - Timber.e("size of array = " + getEmojiArray().length); emojiGridView.setAdapter(new ImageKeyboardAdapter(context, getEmojiArray())); emojiGridView.setOnItemClickListener((parent, view, position, id) -> { if (inputConnection == null) return; String value = emojis.valueAt(position); inputConnection.commitText(value, 1); }); + AppCompatImageButton backspaceButton = (AppCompatImageButton) findViewById(R.id.backspace_button); + backspaceButton.setOnClickListener(view -> { + CharSequence selectedText = inputConnection.getSelectedText(0); + if (TextUtils.isEmpty(selectedText)) + inputConnection.deleteSurroundingText(1, 0); + else + inputConnection.commitText("", 1); + }); } public void setInputConnection(InputConnection inputConnection) { @@ -159,4 +164,8 @@ public class EmojiKeyboard extends LinearLayout { } return emojiArray; } + + public interface EmojiKeyboardOwner { + void setEmojiKeyboardVisible(boolean visible); + } } diff --git a/app/src/main/res/drawable/ic_backspace_black_24dp.xml b/app/src/main/res/drawable/ic_backspace_black_24dp.xml new file mode 100644 index 00000000..5ecf7ae2 --- /dev/null +++ b/app/src/main/res/drawable/ic_backspace_black_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_keyboard_grey_24dp.xml b/app/src/main/res/drawable/ic_keyboard_grey_24dp.xml new file mode 100644 index 00000000..35243dc1 --- /dev/null +++ b/app/src/main/res/drawable/ic_keyboard_grey_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_tag_faces_grey_24dp.xml b/app/src/main/res/drawable/ic_tag_faces_grey_24dp.xml new file mode 100644 index 00000000..e46eab60 --- /dev/null +++ b/app/src/main/res/drawable/ic_tag_faces_grey_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_test_edit_view.xml b/app/src/main/res/layout/activity_test_edit_view.xml index 3b4eded5..11a2e371 100644 --- a/app/src/main/res/layout/activity_test_edit_view.xml +++ b/app/src/main/res/layout/activity_test_edit_view.xml @@ -6,8 +6,8 @@ android:layout_height="match_parent" tools:context=".activities.TestEditView"> - @@ -17,6 +17,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" - android:layout_alignParentBottom="true" /> + android:layout_alignParentBottom="true" + android:visibility="gone"/> \ No newline at end of file diff --git a/app/src/main/res/layout/editor_view.xml b/app/src/main/res/layout/editor_view.xml index 4fa51247..8939ca35 100644 --- a/app/src/main/res/layout/editor_view.xml +++ b/app/src/main/res/layout/editor_view.xml @@ -1,7 +1,8 @@ + xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="wrap_content" android:layout_height="wrap_content"> + android:layout_height="wrap_content" + android:layout_gravity="bottom" + android:padding="4dp" + app:srcCompat="@drawable/ic_tag_faces_grey_24dp" /> diff --git a/app/src/main/res/layout/emoji_keyboard.xml b/app/src/main/res/layout/emoji_keyboard.xml index e8eeb643..6bae615d 100644 --- a/app/src/main/res/layout/emoji_keyboard.xml +++ b/app/src/main/res/layout/emoji_keyboard.xml @@ -1,7 +1,23 @@ + + + + +