4
4
import android .annotation .TargetApi ;
5
5
import android .app .AlertDialog ;
6
6
import android .content .Context ;
7
+ import android .content .DialogInterface ;
7
8
import android .os .Build ;
8
- import android .support .annotation .Nullable ;
9
9
import android .util .AttributeSet ;
10
10
import android .view .LayoutInflater ;
11
11
import android .view .View ;
16
16
import android .widget .LinearLayout ;
17
17
18
18
@ SuppressLint ("AppCompatCustomView" )
19
- public class ReactButton extends Button {
19
+ public class ReactButton extends Button implements View . OnClickListener , View . OnLongClickListener {
20
20
21
- //Alert Dialog To Show Emoji Layout
21
+ //Alert Dialog To Show Emojis
22
22
private AlertDialog alertDialog ;
23
23
//Current React Button
24
24
private Button reactButton = this ;
@@ -31,11 +31,15 @@ public class ReactButton extends Button{
31
31
private ImageButton wowFace ;
32
32
private ImageButton sadFace ;
33
33
private ImageButton angryFace ;
34
+ //User onCLick Implementation
35
+ private OnClickListener onClickListener ;
36
+ //User onLongCLick Implementation
37
+ private OnLongClickListener onDismissListener ;
34
38
35
39
//Check React Button State
36
40
private boolean reactState = false ;
37
41
//Return Type Of Current Emoji
38
- private String emojiType = DEFAULT ;
42
+ private String emojiType = "Default" ;
39
43
40
44
//Emojis Type as String
41
45
public static final String DEFAULT = "Default" ;
@@ -53,35 +57,34 @@ public ReactButton(Context context) {
53
57
super (context );
54
58
reactButton .setCompoundDrawablesWithIntrinsicBounds (R .mipmap .reactbutton_default ,0 ,0 ,0 );
55
59
reactButton .setText (LIKE );
60
+ reactButton .setOnClickListener (this );
61
+ reactButton .setOnLongClickListener (this );
56
62
}
57
63
58
64
public ReactButton (Context context , AttributeSet attrs ) {
59
65
super (context , attrs );
60
66
reactButton .setCompoundDrawablesWithIntrinsicBounds (R .mipmap .reactbutton_default ,0 ,0 ,0 );
61
67
reactButton .setText (LIKE );
68
+ reactButton .setOnClickListener (this );
69
+ reactButton .setOnLongClickListener (this );
62
70
}
63
71
64
72
public ReactButton (Context context , AttributeSet attrs , int defStyleAttr ) {
65
73
super (context , attrs , defStyleAttr );
74
+ reactButton .setOnClickListener (this );
66
75
reactButton .setCompoundDrawablesWithIntrinsicBounds (R .mipmap .reactbutton_default ,0 ,0 ,0 );
67
76
reactButton .setText (LIKE );
77
+ reactButton .setOnClickListener (this );
78
+ reactButton .setOnLongClickListener (this );
68
79
}
69
80
70
81
@ TargetApi (Build .VERSION_CODES .LOLLIPOP )
71
82
public ReactButton (Context context , AttributeSet attrs , int defStyleAttr , int defStyleRes ) {
72
83
super (context , attrs , defStyleAttr , defStyleRes );
73
84
reactButton .setCompoundDrawablesWithIntrinsicBounds (R .mipmap .reactbutton_default ,0 ,0 ,0 );
74
- reactButton .setText (LIKE );
75
- }
76
-
77
- //Initializing Every TextView Using id and View Object
78
- private void initializingTextViews (View currentView ){
79
- likeFace = (ImageButton ) currentView .findViewById (R .id .likeFace );
80
- loveFace = (ImageButton ) currentView .findViewById (R .id .loveFace );
81
- smileFace = (ImageButton ) currentView .findViewById (R .id .smileFace );
82
- wowFace = (ImageButton ) currentView .findViewById (R .id .wowFace );
83
- sadFace = (ImageButton ) currentView .findViewById (R .id .sadFace );
84
- angryFace = (ImageButton ) currentView .findViewById (R .id .angryFace );
85
+ reactButton .setText (layoutColor );
86
+ reactButton .setOnClickListener (this );
87
+ reactButton .setOnLongClickListener (this );
85
88
}
86
89
87
90
//Method To Make Button Like if it default and dislike if state is true
@@ -95,6 +98,42 @@ private void onClickLikeAndDisLike(){
95
98
}
96
99
}
97
100
101
+ //Method To Show Emoji Dialog When User Click Long
102
+ private void onLongClickDialog (){
103
+ //Code When User Click Long on Button
104
+ //Show Dialog With 6 Emoji
105
+ AlertDialog .Builder dialogBuilder = new AlertDialog .Builder (getContext ());
106
+
107
+ //Irrelevant code for customizing the buttons and title
108
+ LayoutInflater inflater = (LayoutInflater ) getContext ().getSystemService ( Context .LAYOUT_INFLATER_SERVICE );
109
+ View dialogView = inflater .inflate (R .layout .emojis_dialog , null );
110
+
111
+ //Initializing LinearLayout For Emojis and Change Attributes
112
+ emojiLayout = dialogView .findViewById (R .id .emojiLayout );
113
+ emojiLayout .setBackgroundColor (layoutColor );
114
+
115
+ initializingTextViews (dialogView );
116
+ onClickTextViews ();
117
+
118
+ dialogBuilder .setView (dialogView );
119
+ alertDialog = dialogBuilder .create ();
120
+
121
+ Window window = alertDialog .getWindow ();
122
+ window .setLayout (WindowManager .LayoutParams .MATCH_PARENT , WindowManager .LayoutParams .WRAP_CONTENT );
123
+
124
+ alertDialog .show ();
125
+ }
126
+
127
+ //Initializing Every TextView Using id and View Object
128
+ private void initializingTextViews (View currentView ){
129
+ likeFace = (ImageButton ) currentView .findViewById (R .id .likeFace );
130
+ loveFace = (ImageButton ) currentView .findViewById (R .id .loveFace );
131
+ smileFace = (ImageButton ) currentView .findViewById (R .id .smileFace );
132
+ wowFace = (ImageButton ) currentView .findViewById (R .id .wowFace );
133
+ sadFace = (ImageButton ) currentView .findViewById (R .id .sadFace );
134
+ angryFace = (ImageButton ) currentView .findViewById (R .id .angryFace );
135
+ }
136
+
98
137
//Set OnClick Method For Every TextView
99
138
private void onClickTextViews (){
100
139
@@ -151,38 +190,17 @@ public void onClick(View view) {
151
190
152
191
}
153
192
154
- //Method To Show Emoji Dialog When User Click Long
155
- private void onLongClickDialog (){
156
- //Code When User Click Long on Button
157
- //Show Dialog With 6 Emoji
158
- AlertDialog .Builder dialogBuilder = new AlertDialog .Builder (getContext ());
159
-
160
- //Irrelevant code for customizing the buttons and title
161
- LayoutInflater inflater = (LayoutInflater ) getContext ().getSystemService ( Context .LAYOUT_INFLATER_SERVICE );
162
- View dialogView = inflater .inflate (R .layout .emojis_dialog , null );
163
-
164
- //Initializing LinearLayout For Emojis and Change Attributes
165
- emojiLayout = dialogView .findViewById (R .id .emojiLayout );
166
- emojiLayout .setBackgroundColor (layoutColor );
167
-
168
- initializingTextViews (dialogView );
169
- onClickTextViews ();
170
-
171
- dialogBuilder .setView (dialogView );
172
- alertDialog = dialogBuilder .create ();
173
-
174
- Window window = alertDialog .getWindow ();
175
- window .setLayout (WindowManager .LayoutParams .MATCH_PARENT , WindowManager .LayoutParams .WRAP_CONTENT );
176
-
177
- alertDialog .show ();
178
- }
179
-
180
193
//Set Custom Color For Emoji Dialog
181
- public void setDialogBackgroundColor (int color ){
194
+ public void dialogBackgroundColor (int color ){
182
195
//Set Color For EmojiDialog
183
196
this .layoutColor = color ;
184
197
}
185
198
199
+ //Return Type Of Current Emoji
200
+ public String getCurrentEmojiType (){
201
+ return emojiType ;
202
+ }
203
+
186
204
//Set The Current Emoji Text And Reaction
187
205
public void setCurrentEmojiType (String react ){
188
206
switch (react ){
@@ -216,11 +234,6 @@ public void setCurrentEmojiType(String react){
216
234
}
217
235
}
218
236
219
- //Return Type Of Current Emoji
220
- public String getCurrentEmojiType (){
221
- return emojiType ;
222
- }
223
-
224
237
//Private Method to Change Button state and set Reaction
225
238
//React Button Default State
226
239
private void reactButtonDefaultState (){
@@ -285,36 +298,43 @@ private void reactButtonAngryState(){
285
298
reactButton .setCompoundDrawablesWithIntrinsicBounds (R .mipmap .reactbutton_angry ,0 ,0 ,0 );
286
299
}
287
300
301
+
302
+ //Get ReactButton OnClick From User
303
+ public void setReactClickListener (OnClickListener onClickListener ){
304
+ this .onClickListener = onClickListener ;
305
+ }
306
+
307
+ //Get ReactButton OnLongClick From User
308
+ public void setReactDismissListener (OnLongClickListener onDismisslListener ){
309
+ this .onDismissListener = onDismisslListener ;
310
+ }
311
+
288
312
@ Override
289
- public void setOnClickListener (@ Nullable OnClickListener onClickListener ) {
290
- //Get Current Listener
291
- final OnClickListener currentListener = onClickListener ;
292
- //Using new OnClick To Merge Library OnClick With Developer OnClick
293
- super .setOnClickListener (new OnClickListener () {
294
- @ Override
295
- public void onClick (View view ) {
296
- //The Developer OnClick Code
297
- currentListener .onClick (view );
298
- //The Library OnClick
299
- onClickLikeAndDisLike ();
300
- }
301
- });
313
+ public void onClick (View view ) {
314
+ //The Library OnClick
315
+ onClickLikeAndDisLike ();
316
+ //If User Set OnClick Using it After Native Library OnClick
317
+ if (onClickListener != null ){
318
+ onClickListener .onClick (view );
319
+ }
302
320
}
303
321
304
322
@ Override
305
- public void setOnLongClickListener (@ Nullable OnLongClickListener onLongClickListener ) {
306
- //Get Current On Long Click on Final Object
307
- final OnLongClickListener currentLongClick = onLongClickListener ;
308
- //Initializing new OnClick To Merge Two OnClick Method
309
- super .setOnLongClickListener (new OnLongClickListener () {
323
+ public boolean onLongClick (View view ) {
324
+ final View currentView = view ;
325
+ //First Using My Native OnLongClick
326
+ onLongClickDialog ();
327
+ //Implement on Dismiss Listener to call Developer Method
328
+ alertDialog .setOnDismissListener (new DialogInterface .OnDismissListener () {
310
329
@ Override
311
- public boolean onLongClick ( View view ) {
312
- //Developer Current Code
313
- currentLongClick . onLongClick ( view );
314
- //Library OnClick Code
315
- onLongClickDialog ( );
316
- return true ;
330
+ public void onDismiss ( DialogInterface dialogInterface ) {
331
+ if ( onDismissListener != null )
332
+ {
333
+ //User OnLongClick Implementation
334
+ onDismissListener . onLongClick ( currentView );
335
+ }
317
336
}
318
337
});
338
+ return true ;
319
339
}
320
340
}
0 commit comments