2222import android .widget .ImageView ;
2323import android .widget .PopupWindow ;
2424
25+ import java .util .ArrayList ;
26+ import java .util .Collections ;
27+ import java .util .List ;
28+
2529import hani .momanii .supernova_emoji_library .Helper .EmojiconEditText ;
2630import hani .momanii .supernova_emoji_library .Helper .EmojiconGridView ;
2731import hani .momanii .supernova_emoji_library .Helper .EmojiconsPopup ;
3236/**
3337 * @author Hani Al Momani (hani.momanii@gmail.com)
3438 */
35- public class EmojIconActions {
39+ public class EmojIconActions implements View . OnFocusChangeListener {
3640
37- boolean useSystemEmoji =false ;
38- EmojiconsPopup popup ;
39- Context context ;
40- View rootView ;
41- ImageView emojiButton ;
42- EmojiconEditText emojiconEditText ;
43- int KeyBoardIcon = R .drawable .ic_action_keyboard ;
44- int SmileyIcons = R .drawable .smiley ;
45- KeyboardListener keyboardListener ;
41+ private boolean useSystemEmoji = false ;
42+ private EmojiconsPopup popup ;
43+ private Context context ;
44+ private View rootView ;
45+ private ImageView emojiButton ;
46+ private int KeyBoardIcon = R .drawable .ic_action_keyboard ;
47+ private int SmileyIcons = R .drawable .smiley ;
48+ private KeyboardListener keyboardListener ;
49+ private List <EmojiconEditText > emojiconEditTextList = new ArrayList <>();
50+ private EmojiconEditText emojiconEditText ;
4651
4752
4853 /**
4954 * Constructor
50- * @param ctx The context of current activity.
51- * @param rootView The top most layout in your view hierarchy. The difference of this view and the screen height will be used to calculate the keyboard height.
55+ *
56+ * @param ctx The context of current activity.
57+ * @param rootView The top most layout in your view hierarchy. The difference of this
58+ * view and the screen height will be used to calculate the keyboard
59+ * height.
5260 * @param emojiconEditText The Id of EditText.
53- * @param emojiButton The Id of ImageButton used to open Emoji
61+ * @param emojiButton The Id of ImageButton used to open Emoji
5462 */
55- public EmojIconActions (Context ctx ,View rootView ,EmojiconEditText emojiconEditText ,ImageView emojiButton )
56- {
57- this .emojiconEditText =emojiconEditText ;
58- this .emojiButton =emojiButton ;
59- this .context =ctx ;
60- this .rootView =rootView ;
61- this .popup = new EmojiconsPopup (rootView , ctx ,useSystemEmoji );
63+ public EmojIconActions (Context ctx , View rootView , EmojiconEditText emojiconEditText ,
64+ ImageView emojiButton ) {
65+ this .emojiButton = emojiButton ;
66+ this .context = ctx ;
67+ this .rootView = rootView ;
68+ addEmojiconEditTextList (emojiconEditText );
69+ this .popup = new EmojiconsPopup (rootView , ctx , useSystemEmoji );
70+ }
71+
72+ public void addEmojiconEditTextList (EmojiconEditText ... emojiconEditText ) {
73+ Collections .addAll (emojiconEditTextList , emojiconEditText );
74+ for (EmojiconEditText editText : emojiconEditText ) {
75+ editText .setOnFocusChangeListener (this );
76+ }
6277 }
6378
6479
6580 /**
6681 * Constructor
67- * @param ctx The context of current activity.
68- * @param rootView The top most layout in your view hierarchy. The difference of this view and the screen height will be used to calculate the keyboard height.
82+ *
83+ * @param ctx The context of current activity.
84+ * @param rootView The top most layout in your view hierarchy. The difference of this
85+ * view and the screen height will be used to calculate the keyboard
86+ * height.
6987 * @param emojiconEditText The Id of EditText.
70- * @param emojiButton The Id of ImageButton used to open Emoji
88+ * @param emojiButton The Id of ImageButton used to open Emoji
7189 * @param iconPressedColor The color of icons on tab
72- * @param tabsColor The color of tabs background
73- * @param backgroundColor The color of emoji background
90+ * @param tabsColor The color of tabs background
91+ * @param backgroundColor The color of emoji background
7492 */
75- public EmojIconActions (Context ctx ,View rootView ,EmojiconEditText emojiconEditText ,ImageView emojiButton ,String iconPressedColor ,String tabsColor ,String backgroundColor )
76- {
77- this .emojiconEditText =emojiconEditText ;
78- this .emojiButton =emojiButton ;
79- this .context =ctx ;
80- this .rootView =rootView ;
81- this .popup = new EmojiconsPopup (rootView , ctx ,useSystemEmoji ,iconPressedColor ,tabsColor ,backgroundColor );
93+ public EmojIconActions (Context ctx , View rootView , EmojiconEditText emojiconEditText ,
94+ ImageView emojiButton , String iconPressedColor , String tabsColor ,
95+ String backgroundColor ) {
96+ addEmojiconEditTextList (emojiconEditText );
97+ this .emojiButton = emojiButton ;
98+ this .context = ctx ;
99+ this .rootView = rootView ;
100+ this .popup = new EmojiconsPopup (rootView , ctx , useSystemEmoji , iconPressedColor ,
101+ tabsColor , backgroundColor );
82102 }
83103
84- public void setIconsIds (int keyboardIcon ,int smileyIcon )
85- {
86- this .KeyBoardIcon =keyboardIcon ;
87- this .SmileyIcons =smileyIcon ;
104+ public void setIconsIds (int keyboardIcon , int smileyIcon ) {
105+ this .KeyBoardIcon = keyboardIcon ;
106+ this .SmileyIcons = smileyIcon ;
88107 }
89108
90- public void setUseSystemEmoji (boolean useSystemEmoji )
91- {
92- this .useSystemEmoji =useSystemEmoji ;
93- this .emojiconEditText .setUseSystemDefault (useSystemEmoji );
109+ public void setUseSystemEmoji (boolean useSystemEmoji ) {
110+ this .useSystemEmoji = useSystemEmoji ;
111+ for (EmojiconEditText editText : emojiconEditTextList ) {
112+ editText .setUseSystemDefault (useSystemEmoji );
113+ }
94114 refresh ();
95115 }
96116
97- private void refresh ()
98- {
99- popup .updateUseSystemDefault (useSystemEmoji );
100117
118+ private void refresh () {
119+ popup .updateUseSystemDefault (useSystemEmoji );
101120 }
102121
103-
104- public void ShowEmojIcon ( )
105- {
106-
122+ public void ShowEmojIcon () {
123+ if (emojiconEditText == null )
124+ emojiconEditText = emojiconEditTextList .get (0 );
107125 //Will automatically set size according to the soft keyboard size
108126 popup .setSizeForSoftKeyboard ();
109127
@@ -112,12 +130,13 @@ public void ShowEmojIcon( )
112130
113131 @ Override
114132 public void onDismiss () {
115- changeEmojiKeyboardIcon (emojiButton ,SmileyIcons );
133+ changeEmojiKeyboardIcon (emojiButton , SmileyIcons );
116134 }
117135 });
118136
119137 //If the text keyboard closes, also dismiss the emoji popup
120- popup .setOnSoftKeyboardOpenCloseListener (new EmojiconsPopup .OnSoftKeyboardOpenCloseListener () {
138+ popup .setOnSoftKeyboardOpenCloseListener (new EmojiconsPopup
139+ .OnSoftKeyboardOpenCloseListener () {
121140
122141 @ Override
123142 public void onKeyboardOpen (int keyBoardHeight ) {
@@ -129,7 +148,7 @@ public void onKeyboardOpen(int keyBoardHeight) {
129148 public void onKeyboardClose () {
130149 if (keyboardListener != null )
131150 keyboardListener .onKeyboardClose ();
132- if (popup .isShowing ())
151+ if (popup .isShowing ())
133152 popup .dismiss ();
134153 }
135154 });
@@ -139,7 +158,7 @@ public void onKeyboardClose() {
139158
140159 @ Override
141160 public void onEmojiconClicked (Emojicon emojicon ) {
142- if (emojiconEditText == null || emojicon == null ) {
161+ if (emojicon == null ) {
143162 return ;
144163 }
145164
@@ -156,7 +175,8 @@ public void onEmojiconClicked(Emojicon emojicon) {
156175 });
157176
158177 //On backspace clicked, emulate the KEYCODE_DEL key event
159- popup .setOnEmojiconBackspaceClickedListener (new EmojiconsPopup .OnEmojiconBackspaceClickedListener () {
178+ popup .setOnEmojiconBackspaceClickedListener (new EmojiconsPopup
179+ .OnEmojiconBackspaceClickedListener () {
160180
161181 @ Override
162182 public void onEmojiconBackspaceClicked (View v ) {
@@ -167,62 +187,78 @@ public void onEmojiconBackspaceClicked(View v) {
167187 });
168188
169189 // To toggle between text keyboard and emoji keyboard keyboard(Popup)
190+ showForEditText ();
191+ }
192+
193+ private void showForEditText () {
194+
170195 emojiButton .setOnClickListener (new View .OnClickListener () {
171196
172197 @ Override
173198 public void onClick (View v ) {
174-
199+ if (emojiconEditText == null )
200+ emojiconEditText = emojiconEditTextList .get (0 );
175201 //If popup is not showing => emoji keyboard is not visible, we need to show it
176- if (!popup .isShowing ()){
202+ if (!popup .isShowing ()) {
177203
178204 //If keyboard is visible, simply show the emoji popup
179- if (popup .isKeyBoardOpen ()){
205+ if (popup .isKeyBoardOpen ()) {
180206 popup .showAtBottom ();
181- changeEmojiKeyboardIcon (emojiButton ,KeyBoardIcon );
207+ changeEmojiKeyboardIcon (emojiButton , KeyBoardIcon );
182208 }
183209
184- //else, open the text keyboard first and immediately after that show the emoji popup
185- else {
210+ //else, open the text keyboard first and immediately after that show the
211+ // emoji popup
212+ else {
186213 emojiconEditText .setFocusableInTouchMode (true );
187214 emojiconEditText .requestFocus ();
188- final InputMethodManager inputMethodManager = (InputMethodManager ) context .getSystemService (Context .INPUT_METHOD_SERVICE );
189- inputMethodManager .showSoftInput (emojiconEditText , InputMethodManager .SHOW_IMPLICIT );
215+ final InputMethodManager inputMethodManager = (InputMethodManager )
216+ context .getSystemService (Context .INPUT_METHOD_SERVICE );
217+ inputMethodManager .showSoftInput (emojiconEditText , InputMethodManager
218+ .SHOW_IMPLICIT );
190219 popup .showAtBottomPending ();
191- changeEmojiKeyboardIcon (emojiButton ,KeyBoardIcon );
220+ changeEmojiKeyboardIcon (emojiButton , KeyBoardIcon );
192221 }
193222 }
194223
195224 //If popup is showing, simply dismiss it to show the undelying text keyboard
196- else {
225+ else {
197226 popup .dismiss ();
198227 }
199228
200229
201230 }
202231 });
203-
204232 }
205233
206234
207- public void closeEmojIcon ()
208- {
209- if (popup !=null &&popup .isShowing ())
210- popup .dismiss ();
235+ public void closeEmojIcon () {
236+ if (popup != null && popup .isShowing ())
237+ popup .dismiss ();
211238
212239 }
213240
214- private void changeEmojiKeyboardIcon (ImageView iconToBeChanged , int drawableResourceId ){
241+ private void changeEmojiKeyboardIcon (ImageView iconToBeChanged , int drawableResourceId ) {
215242 iconToBeChanged .setImageResource (drawableResourceId );
216243 }
217244
245+ @ Override
246+ public void onFocusChange (View view , boolean hasFocus ) {
247+ if (hasFocus ) {
248+ if (view instanceof EmojiconEditText ) {
249+ emojiconEditText = (EmojiconEditText ) view ;
250+ }
251+ }
252+ }
218253
219254
220- public interface KeyboardListener {
255+ public interface KeyboardListener {
221256 void onKeyboardOpen ();
257+
222258 void onKeyboardClose ();
223259 }
224260
225- public void setKeyboardListener (KeyboardListener listener ){
261+ public void setKeyboardListener (KeyboardListener listener ) {
226262 this .keyboardListener = listener ;
227263 }
228264
0 commit comments