diff options
author | Ankush Sachdeva <sankush@yahoo.co.in> | 2015-05-09 01:07:49 +0530 |
---|---|---|
committer | Ankush Sachdeva <sankush@yahoo.co.in> | 2015-05-09 01:07:49 +0530 |
commit | 7284b7d42370b33c64b3809dd5e2e2833f445af8 (patch) | |
tree | 58ba929f3295f873776a858f700bf464eab7fadc /example | |
parent | 58545344199e6b291595bbd8fc3ab9169b4fcd62 (diff) |
Add emojicon in between text when required; fixes#19
Diffstat (limited to '')
-rw-r--r-- | example/src/com/example/emojiconsample/MainActivity.java | 60 |
1 files changed, 26 insertions, 34 deletions
diff --git a/example/src/com/example/emojiconsample/MainActivity.java b/example/src/com/example/emojiconsample/MainActivity.java index 247eb30..4d4fb92 100644 --- a/example/src/com/example/emojiconsample/MainActivity.java +++ b/example/src/com/example/emojiconsample/MainActivity.java @@ -39,26 +39,6 @@ public class MainActivity extends Activity { //Will automatically set size according to the soft keyboard size popup.setSizeForSoftKeyboard(); - //Set on emojicon click listener - popup.setOnEmojiconClickedListener(new OnEmojiconClickedListener() { - - @Override - public void onEmojiconClicked(Emojicon emojicon) { - emojiconEditText.append(emojicon.getEmoji()); - } - }); - - //Set on backspace click listener - popup.setOnEmojiconBackspaceClickedListener(new OnEmojiconBackspaceClickedListener() { - - @Override - public void onEmojiconBackspaceClicked(View v) { - KeyEvent event = new KeyEvent( - 0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL); - emojiconEditText.dispatchKeyEvent(event); - } - }); - //If the emoji popup is dismissed, change emojiButton to smiley icon popup.setOnDismissListener(new OnDismissListener() { @@ -88,7 +68,19 @@ public class MainActivity extends Activity { @Override public void onEmojiconClicked(Emojicon emojicon) { - emojiconEditText.append(emojicon.getEmoji()); + if (emojiconEditText == null || emojicon == null) { + return; + } + + int start = emojiconEditText.getSelectionStart(); + int end = emojiconEditText.getSelectionEnd(); + if (start < 0) { + emojiconEditText.append(emojicon.getEmoji()); + } else { + emojiconEditText.getText().replace(Math.min(start, end), + Math.max(start, end), emojicon.getEmoji(), 0, + emojicon.getEmoji().length()); + } } }); @@ -98,7 +90,7 @@ public class MainActivity extends Activity { @Override public void onEmojiconBackspaceClicked(View v) { KeyEvent event = new KeyEvent( - 0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL); + 0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL); emojiconEditText.dispatchKeyEvent(event); } }); @@ -137,21 +129,21 @@ public class MainActivity extends Activity { }); //On submit, add the edittext text to listview and clear the edittext - submitButton.setOnClickListener(new OnClickListener() { +submitButton.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - String newText = emojiconEditText.getText().toString(); - emojiconEditText.getText().clear(); - mAdapter.add(newText); - mAdapter.notifyDataSetChanged(); + @Override + public void onClick(View v) { + String newText = emojiconEditText.getText().toString(); + emojiconEditText.getText().clear(); + mAdapter.add(newText); + mAdapter.notifyDataSetChanged(); - } - }); } +}); +} - private void changeEmojiKeyboardIcon(ImageView iconToBeChanged, int drawableResourceId){ - iconToBeChanged.setImageResource(drawableResourceId); - } +private void changeEmojiKeyboardIcon(ImageView iconToBeChanged, int drawableResourceId){ + iconToBeChanged.setImageResource(drawableResourceId); +} } |