18
18
import android .widget .Button ;
19
19
import android .widget .GridView ;
20
20
21
+ import androidx .annotation .DrawableRes ;
22
+
21
23
import java .util .ArrayList ;
22
24
import java .util .Arrays ;
23
25
import java .util .List ;
@@ -69,6 +71,18 @@ public class ReactButton
69
71
*/
70
72
private int mReactDialogShape = R .drawable .react_dialog_shape ;
71
73
74
+ /**
75
+ * The size of reaction icon in dp
76
+ * Icon size + icon padding * 2
77
+ */
78
+ private static final int ICON_SIZE_WITH_PADDING = 55 ;
79
+
80
+ /**
81
+ * Full reaction icon size converted from dp
82
+ */
83
+ private final int REACTION_ICON_SIZE = (ICON_SIZE_WITH_PADDING *
84
+ getResources ().getDisplayMetrics ().densityDpi / DisplayMetrics .DENSITY_DEFAULT );
85
+
72
86
public interface OnReactionDialogStateListener {
73
87
void onDialogOpened ();
74
88
@@ -133,15 +147,18 @@ private void onReactionButtonClick() {
133
147
* Show Reaction dialog when user long click on react button
134
148
*/
135
149
private void showReactionsDialog () {
136
- AlertDialog .Builder dialogBuilder = new AlertDialog .Builder (getContext ());
137
-
138
- LayoutInflater inflater = (LayoutInflater ) getContext ().getSystemService (Context .LAYOUT_INFLATER_SERVICE );
150
+ final Context context = getContext ();
151
+ LayoutInflater inflater = (LayoutInflater ) context .getSystemService (Context .LAYOUT_INFLATER_SERVICE );
139
152
View dialogView = inflater .inflate (R .layout .react_dialog_layout , null );
140
- GridView reactionsGrid = dialogView .findViewById (R .id .reactionsList );
141
153
142
- ReactionAdapter adapter = new ReactionAdapter (getContext (), mReactions );
154
+ GridView reactionsGrid = dialogView .findViewById (R .id .reactionsList );
155
+ ReactionAdapter adapter = new ReactionAdapter (context , mReactions );
143
156
reactionsGrid .setAdapter (adapter );
144
157
158
+ // Setup the columns number
159
+ if (mDialogColumnsNumber == 0 ) mDialogColumnsNumber = mReactions .size ();
160
+ reactionsGrid .setNumColumns (mDialogColumnsNumber );
161
+
145
162
reactionsGrid .setOnItemClickListener (new AdapterView .OnItemClickListener () {
146
163
@ Override
147
164
public void onItemClick (AdapterView <?> parent , View v , int position , long id ) {
@@ -151,29 +168,24 @@ public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
151
168
}
152
169
});
153
170
171
+ AlertDialog .Builder dialogBuilder = new AlertDialog .Builder (context );
154
172
dialogBuilder .setView (dialogView );
155
173
mReactAlertDialog = dialogBuilder .create ();
156
174
mReactAlertDialog .requestWindowFeature (Window .FEATURE_NO_TITLE );
157
175
158
176
Window window = mReactAlertDialog .getWindow ();
159
- window .setGravity (Gravity .CENTER );
160
177
window .setBackgroundDrawableResource (mReactDialogShape );
178
+
179
+ // Setup dialog gravity and dynamic position
161
180
WindowManager .LayoutParams windowManagerAttributes = window .getAttributes ();
162
181
windowManagerAttributes .gravity = Gravity .TOP | Gravity .START ;
163
182
windowManagerAttributes .x = (int ) getX () + (getWidth () / 2 );
164
183
windowManagerAttributes .y = (int ) getY () + (getHeight () / 2 );
165
184
166
- if (mDialogColumnsNumber == 0 ) mDialogColumnsNumber = mReactions .size ();
167
-
168
- reactionsGrid .setNumColumns (mDialogColumnsNumber );
169
-
170
185
mReactAlertDialog .show ();
171
186
if (mOnReactionDialogStateListener != null ) mOnReactionDialogStateListener .onDialogOpened ();
172
187
173
- int iconFullSize = 55 ; // Icon size + icon padding * 2
174
- int reactIconSize = (iconFullSize * getResources ().getDisplayMetrics ().densityDpi / DisplayMetrics .DENSITY_DEFAULT );
175
-
176
- int dialogWidth = reactIconSize * mDialogColumnsNumber ;
188
+ int dialogWidth = REACTION_ICON_SIZE * mDialogColumnsNumber ;
177
189
window .setLayout (dialogWidth , WindowManager .LayoutParams .WRAP_CONTENT );
178
190
179
191
mReactAlertDialog .setOnDismissListener (new DialogInterface .OnDismissListener () {
@@ -187,10 +199,10 @@ public void onDismiss(DialogInterface dialogInterface) {
187
199
}
188
200
189
201
/**
190
- * @param shapeId : set xml Shape for react dialog layout
202
+ * @param drawableShape : set xml Shape for react dialog layout
191
203
*/
192
- public void setReactionDialogShape (int shapeId ) {
193
- this .mReactDialogShape = shapeId ;
204
+ public void setReactionDialogShape (@ DrawableRes int drawableShape ) {
205
+ this .mReactDialogShape = drawableShape ;
194
206
}
195
207
196
208
/**
0 commit comments