Skip to content

Commit a07c213

Browse files
authored
Add SetCurrentEmoji Method
1 parent a93efe5 commit a07c213

File tree

1 file changed

+91
-71
lines changed

1 file changed

+91
-71
lines changed

reactbutton/src/main/java/com/amrdeveloper/reactbutton/ReactButton.java

Lines changed: 91 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import android.annotation.TargetApi;
55
import android.app.AlertDialog;
66
import android.content.Context;
7+
import android.content.DialogInterface;
78
import android.os.Build;
8-
import android.support.annotation.Nullable;
99
import android.util.AttributeSet;
1010
import android.view.LayoutInflater;
1111
import android.view.View;
@@ -16,9 +16,9 @@
1616
import android.widget.LinearLayout;
1717

1818
@SuppressLint("AppCompatCustomView")
19-
public class ReactButton extends Button{
19+
public class ReactButton extends Button implements View.OnClickListener , View.OnLongClickListener{
2020

21-
//Alert Dialog To Show Emoji Layout
21+
//Alert Dialog To Show Emojis
2222
private AlertDialog alertDialog;
2323
//Current React Button
2424
private Button reactButton = this;
@@ -31,11 +31,15 @@ public class ReactButton extends Button{
3131
private ImageButton wowFace;
3232
private ImageButton sadFace;
3333
private ImageButton angryFace;
34+
//User onCLick Implementation
35+
private OnClickListener onClickListener;
36+
//User onLongCLick Implementation
37+
private OnLongClickListener onDismissListener;
3438

3539
//Check React Button State
3640
private boolean reactState = false;
3741
//Return Type Of Current Emoji
38-
private String emojiType = DEFAULT;
42+
private String emojiType = "Default";
3943

4044
//Emojis Type as String
4145
public static final String DEFAULT = "Default";
@@ -53,35 +57,34 @@ public ReactButton(Context context) {
5357
super(context);
5458
reactButton.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.reactbutton_default,0,0,0);
5559
reactButton.setText(LIKE);
60+
reactButton.setOnClickListener(this);
61+
reactButton.setOnLongClickListener(this);
5662
}
5763

5864
public ReactButton(Context context, AttributeSet attrs) {
5965
super(context, attrs);
6066
reactButton.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.reactbutton_default,0,0,0);
6167
reactButton.setText(LIKE);
68+
reactButton.setOnClickListener(this);
69+
reactButton.setOnLongClickListener(this);
6270
}
6371

6472
public ReactButton(Context context, AttributeSet attrs, int defStyleAttr) {
6573
super(context, attrs, defStyleAttr);
74+
reactButton.setOnClickListener(this);
6675
reactButton.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.reactbutton_default,0,0,0);
6776
reactButton.setText(LIKE);
77+
reactButton.setOnClickListener(this);
78+
reactButton.setOnLongClickListener(this);
6879
}
6980

7081
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
7182
public ReactButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
7283
super(context, attrs, defStyleAttr, defStyleRes);
7384
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);
8588
}
8689

8790
//Method To Make Button Like if it default and dislike if state is true
@@ -95,6 +98,42 @@ private void onClickLikeAndDisLike(){
9598
}
9699
}
97100

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+
98137
//Set OnClick Method For Every TextView
99138
private void onClickTextViews(){
100139

@@ -151,38 +190,17 @@ public void onClick(View view) {
151190

152191
}
153192

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-
180193
//Set Custom Color For Emoji Dialog
181-
public void setDialogBackgroundColor(int color){
194+
public void dialogBackgroundColor(int color){
182195
//Set Color For EmojiDialog
183196
this.layoutColor = color;
184197
}
185198

199+
//Return Type Of Current Emoji
200+
public String getCurrentEmojiType(){
201+
return emojiType;
202+
}
203+
186204
//Set The Current Emoji Text And Reaction
187205
public void setCurrentEmojiType(String react){
188206
switch (react){
@@ -216,11 +234,6 @@ public void setCurrentEmojiType(String react){
216234
}
217235
}
218236

219-
//Return Type Of Current Emoji
220-
public String getCurrentEmojiType(){
221-
return emojiType;
222-
}
223-
224237
//Private Method to Change Button state and set Reaction
225238
//React Button Default State
226239
private void reactButtonDefaultState(){
@@ -285,36 +298,43 @@ private void reactButtonAngryState(){
285298
reactButton.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.reactbutton_angry,0,0,0);
286299
}
287300

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+
288312
@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+
}
302320
}
303321

304322
@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() {
310329
@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+
}
317336
}
318337
});
338+
return true;
319339
}
320340
}

0 commit comments

Comments
 (0)