Browse Source

finish emoji keyboard behavior

pull/45/head
Thodoris1999 6 years ago
parent
commit
1bf1b1a6f8
  1. 34
      app/src/main/java/gr/thmmy/mthmmy/activities/TestEditView.java
  2. 112
      app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java
  3. 21
      app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java
  4. 5
      app/src/main/res/drawable/ic_backspace_black_24dp.xml
  5. 5
      app/src/main/res/drawable/ic_keyboard_grey_24dp.xml
  6. 5
      app/src/main/res/drawable/ic_tag_faces_grey_24dp.xml
  7. 7
      app/src/main/res/layout/activity_test_edit_view.xml
  8. 12
      app/src/main/res/layout/editor_view.xml
  9. 16
      app/src/main/res/layout/emoji_keyboard.xml

34
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.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.text.InputType; import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import android.widget.EditText;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.utils.EditorView;
import gr.thmmy.mthmmy.utils.EmojiKeyboard; import gr.thmmy.mthmmy.utils.EmojiKeyboard;
public class TestEditView extends AppCompatActivity { public class TestEditView extends AppCompatActivity implements EmojiKeyboard.EmojiKeyboardOwner {
EmojiKeyboard emojiKeyboard;
EditorView editorView;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_edit_view); setContentView(R.layout.activity_test_edit_view);
EditText testEdittext = (EditText) findViewById(R.id.test_edittext); editorView = (EditorView) findViewById(R.id.editor_view);
EmojiKeyboard emojiKeyboard = (EmojiKeyboard) findViewById(R.id.emoji_keyboard); emojiKeyboard = (EmojiKeyboard) findViewById(R.id.emoji_keyboard);
testEdittext.setRawInputType(InputType.TYPE_CLASS_TEXT); InputConnection ic = editorView.getInputConnection();
testEdittext.setTextIsSelectable(true);
InputConnection ic = testEdittext.onCreateInputConnection(new EditorInfo());
emojiKeyboard.setInputConnection(ic); 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();
}
} }
} }

112
app/src/main/java/gr/thmmy/mthmmy/utils/EditorView.java

@ -1,39 +1,67 @@
package gr.thmmy.mthmmy.utils; package gr.thmmy.mthmmy.utils;
import android.app.Activity;
import android.content.Context; 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.support.v7.widget.AppCompatImageButton;
import android.text.Editable; import android.text.Editable;
import android.util.AttributeSet;
import android.view.LayoutInflater; 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.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
public class EditorView extends RelativeLayout implements KeyboardView.OnKeyboardActionListener { public class EditorView extends LinearLayout {
public final static int SMILE = 10;
private EditText editText; private EditText editText;
AppCompatImageButton emojiButton;
private AppCompatImageButton submitButton; private AppCompatImageButton submitButton;
private EmojiKeyboard.EmojiKeyboardOwner emojiKeyboardOwner;
private boolean emojiKeyboardVisible = false;
public EditorView(Context context) { public EditorView(Context context) {
super(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); private void init(Context context, AttributeSet attrs) {
inflater.inflate(R.layout.editor_view, this, true); setOrientation(VERTICAL);
LayoutInflater.from(context).inflate(R.layout.editor_view, this, true);
editText = (EditText) findViewById(R.id.editor_edittext); 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); 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() { public Editable getText() {
@ -48,53 +76,21 @@ public class EditorView extends RelativeLayout implements KeyboardView.OnKeyboar
submitButton.setOnClickListener(onSubmitListener); submitButton.setOnClickListener(onSubmitListener);
} }
@Override public void setEmojiKeyboardOwner(EmojiKeyboard.EmojiKeyboardOwner emojiKeyboardOwner) {
public void onPress(int primaryCode) { this.emojiKeyboardOwner = emojiKeyboardOwner;
} }
@Override public InputConnection getInputConnection() {
public void onRelease(int primaryCode) { return editText.onCreateInputConnection(new EditorInfo());
} }
@Override public void notifyKeyboardVisibility(boolean visible) {
public void onKey(int primaryCode, int[] keyCodes) { if (visible) {
Editable editable = editText.getText(); emojiButton.setImageResource(R.drawable.ic_keyboard_grey_24dp);
if (editText.hasSelection()) emojiKeyboardVisible = true;
editable.delete(editText.getSelectionStart(), editText.getSelectionEnd()); } else {
int cursorIndex = editText.getSelectionStart(); emojiButton.setImageResource(R.drawable.ic_tag_faces_grey_24dp);
String appendedText = ""; emojiKeyboardVisible = false;
switch (primaryCode) {
case SMILE:
appendedText = "^:)^";
break;
} }
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() {
} }
} }

21
app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java

@ -1,6 +1,8 @@
package gr.thmmy.mthmmy.utils; package gr.thmmy.mthmmy.utils;
import android.content.Context; import android.content.Context;
import android.support.v7.widget.AppCompatImageButton;
import android.text.TextUtils;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -35,7 +37,7 @@ public class EmojiKeyboard extends LinearLayout {
// add space before emoji // add space before emoji
emojis.append(R.drawable.heart, "<3"); 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.locked, "^lock^");
emojis.append(R.drawable.roll_over, "^rollover^"); emojis.append(R.drawable.roll_over, "^rollover^");
emojis.append(R.drawable.redface, "^redface^"); 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.mad, "^mad^");
emojis.append(R.drawable.wav, "^wav^"); emojis.append(R.drawable.wav, "^wav^");
emojis.append(R.drawable.binkybaby, "^binkybaby^"); emojis.append(R.drawable.binkybaby, "^binkybaby^");
// maybe renamed
emojis.append(R.drawable.police, "^police^"); emojis.append(R.drawable.police, "^police^");
emojis.append(R.drawable.dontknow, "^dontknow^"); emojis.append(R.drawable.dontknow, "^dontknow^");
// the next two are the same thing? // 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_hot, "^angryhot^");
emojis.append(R.drawable.angry, "^angry^"); emojis.append(R.drawable.angry, "^angry^");
emojis.append(R.drawable.foyska, "^fouska^"); 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.e10_7_3e, "^sfinaki^");
emojis.append(R.drawable.bang_head, "^banghead^"); emojis.append(R.drawable.bang_head, "^banghead^");
emojis.append(R.drawable.crybaby, "^crybaby^"); 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.jerk, "^jerk^");
emojis.append(R.drawable.nono, "^nono^"); emojis.append(R.drawable.nono, "^nono^");
emojis.append(R.drawable.notworthy, "^notworthy^"); 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.off_topic, "^off-topic^");
emojis.append(R.drawable.puke, "^puke^"); emojis.append(R.drawable.puke, "^puke^");
emojis.append(R.drawable.shout, "^shout^"); 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.seestars, "^seestars^");
emojis.append(R.drawable.sfyri, "^sfyri^"); emojis.append(R.drawable.sfyri, "^sfyri^");
emojis.append(R.drawable.spam2, "^spam^"); emojis.append(R.drawable.spam2, "^spam^");
// changed icon name to become valid drawable name
emojis.append(R.drawable.esuper, "^super^"); emojis.append(R.drawable.esuper, "^super^");
emojis.append(R.drawable.tafos, "^tafos^"); emojis.append(R.drawable.tafos, "^tafos^");
emojis.append(R.drawable.tomatomourh, "^tomato^"); emojis.append(R.drawable.tomatomourh, "^tomato^");
@ -139,13 +137,20 @@ public class EmojiKeyboard extends LinearLayout {
emojis.append(R.drawable.smurf, "^smurf^"); emojis.append(R.drawable.smurf, "^smurf^");
GridView emojiGridView = (GridView) findViewById(R.id.emoji_gridview); GridView emojiGridView = (GridView) findViewById(R.id.emoji_gridview);
Timber.e("size of array = " + getEmojiArray().length);
emojiGridView.setAdapter(new ImageKeyboardAdapter(context, getEmojiArray())); emojiGridView.setAdapter(new ImageKeyboardAdapter(context, getEmojiArray()));
emojiGridView.setOnItemClickListener((parent, view, position, id) -> { emojiGridView.setOnItemClickListener((parent, view, position, id) -> {
if (inputConnection == null) return; if (inputConnection == null) return;
String value = emojis.valueAt(position); String value = emojis.valueAt(position);
inputConnection.commitText(value, 1); 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) { public void setInputConnection(InputConnection inputConnection) {
@ -159,4 +164,8 @@ public class EmojiKeyboard extends LinearLayout {
} }
return emojiArray; return emojiArray;
} }
public interface EmojiKeyboardOwner {
void setEmojiKeyboardVisible(boolean visible);
}
} }

5
app/src/main/res/drawable/ic_backspace_black_24dp.xml

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#4B4B4B"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M22,3L7,3c-0.69,0 -1.23,0.35 -1.59,0.88L0,12l5.41,8.11c0.36,0.53 0.9,0.89 1.59,0.89h15c1.1,0 2,-0.9 2,-2L24,5c0,-1.1 -0.9,-2 -2,-2zM19,15.59L17.59,17 14,13.41 10.41,17 9,15.59 12.59,12 9,8.41 10.41,7 14,10.59 17.59,7 19,8.41 15.41,12 19,15.59z"/>
</vector>

5
app/src/main/res/drawable/ic_keyboard_grey_24dp.xml

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#4B4B4B"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M20,5L4,5c-1.1,0 -1.99,0.9 -1.99,2L2,17c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,7c0,-1.1 -0.9,-2 -2,-2zM11,8h2v2h-2L11,8zM11,11h2v2h-2v-2zM8,8h2v2L8,10L8,8zM8,11h2v2L8,13v-2zM7,13L5,13v-2h2v2zM7,10L5,10L5,8h2v2zM16,17L8,17v-2h8v2zM16,13h-2v-2h2v2zM16,10h-2L14,8h2v2zM19,13h-2v-2h2v2zM19,10h-2L17,8h2v2z"/>
</vector>

5
app/src/main/res/drawable/ic_tag_faces_grey_24dp.xml

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#4B4B4B"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8zM15.5,11c0.83,0 1.5,-0.67 1.5,-1.5S16.33,8 15.5,8 14,8.67 14,9.5s0.67,1.5 1.5,1.5zM8.5,11c0.83,0 1.5,-0.67 1.5,-1.5S9.33,8 8.5,8 7,8.67 7,9.5 7.67,11 8.5,11zM12,17.5c2.33,0 4.31,-1.46 5.11,-3.5L6.89,14c0.8,2.04 2.78,3.5 5.11,3.5z"/>
</vector>

7
app/src/main/res/layout/activity_test_edit_view.xml

@ -6,8 +6,8 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".activities.TestEditView"> tools:context=".activities.TestEditView">
<EditText <gr.thmmy.mthmmy.utils.EditorView
android:id="@+id/test_edittext" android:id="@+id/editor_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true" /> android:layout_alignParentTop="true" />
@ -17,6 +17,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
android:layout_alignParentBottom="true" /> android:layout_alignParentBottom="true"
android:visibility="gone"/>
</RelativeLayout> </RelativeLayout>

12
app/src/main/res/layout/editor_view.xml

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<merge <merge
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"> xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout <LinearLayout
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -32,16 +33,17 @@
<android.support.v7.widget.AppCompatImageButton <android.support.v7.widget.AppCompatImageButton
android:id="@+id/emoji_keyboard_button" android:id="@+id/emoji_keyboard_button"
android:layout_width="wrap_content" 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" />
<android.support.v7.widget.AppCompatImageButton <android.support.v7.widget.AppCompatImageButton
android:id="@+id/submit_button" android:id="@+id/submit_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom" android:layout_gravity="bottom"
android:layout_marginBottom="5dp" android:padding="4dp"
android:layout_marginEnd="5dp"
android:background="@color/card_background"
android:contentDescription="@string/submit" android:contentDescription="@string/submit"
app:srcCompat="@drawable/ic_send_accent_24dp" /> app:srcCompat="@drawable/ic_send_accent_24dp" />
</LinearLayout> </LinearLayout>

16
app/src/main/res/layout/emoji_keyboard.xml

@ -1,7 +1,23 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" <merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="200dp"> android:layout_width="match_parent" android:layout_height="200dp">
<!--black border line-->
<View
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="1dp" />
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/backspace_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_backspace_black_24dp"
android:padding="2dp"
android:layout_marginEnd="8dp"
android:layout_gravity="end" />
<GridView <GridView
android:id="@+id/emoji_gridview" android:id="@+id/emoji_gridview"
android:layout_width="match_parent" android:layout_width="match_parent"

Loading…
Cancel
Save