27
27
@ SuppressWarnings ("unused" )
28
28
public class NumberKeyboard extends GridLayout {
29
29
30
- private static final int DEFAULT_KEY_WIDTH_DP = 70 ;
31
- private static final int DEFAULT_KEY_HEIGHT_DP = 70 ;
32
- private static final int DEFAULT_KEY_TEXT_SIZE_SP = 32 ;
30
+ private static final int DEFAULT_KEY_WIDTH_DP = - 1 ; // match_parent
31
+ private static final int DEFAULT_KEY_HEIGHT_DP = - 1 ; // match_parent
32
+ private static final int DEFAULT_KEY_PADDING_DP = 16 ;
33
33
34
34
static {
35
35
AppCompatDelegate .setCompatVectorFromResourcesEnabled (true );
@@ -39,10 +39,10 @@ public class NumberKeyboard extends GridLayout {
39
39
private int keyWidth ;
40
40
@ Dimension
41
41
private int keyHeight ;
42
+ @ Dimension
43
+ private int keyPadding ;
42
44
@ DrawableRes
43
45
private int numberKeyBackground ;
44
- @ Dimension
45
- private int numberKeyTextSize ;
46
46
@ ColorRes
47
47
private int numberKeyTextColor ;
48
48
@ DrawableRes
@@ -116,6 +116,9 @@ public void showRightAuxButton() {
116
116
* Sets key width in px.
117
117
*/
118
118
public void setKeyWidth (int px ) {
119
+ if (px == DEFAULT_KEY_WIDTH_DP ) {
120
+ return ;
121
+ }
119
122
for (TextView key : numericKeys ) {
120
123
key .getLayoutParams ().width = px ;
121
124
}
@@ -128,6 +131,9 @@ public void setKeyWidth(int px) {
128
131
* Sets key height in px.
129
132
*/
130
133
public void setKeyHeight (int px ) {
134
+ if (px == DEFAULT_KEY_HEIGHT_DP ) {
135
+ return ;
136
+ }
131
137
for (TextView key : numericKeys ) {
132
138
key .getLayoutParams ().height = px ;
133
139
}
@@ -137,20 +143,23 @@ public void setKeyHeight(int px) {
137
143
}
138
144
139
145
/**
140
- * Sets number keys background .
146
+ * Sets key padding in px .
141
147
*/
142
- public void setNumberKeyBackground ( @ DrawableRes int background ) {
148
+ public void setKeyPadding ( int px ) {
143
149
for (TextView key : numericKeys ) {
144
- key .setBackground (ContextCompat .getDrawable (getContext (), background ));
150
+ key .setPadding (px , px , px , px );
151
+ key .setCompoundDrawablePadding (-1 * px );
145
152
}
153
+ leftAuxBtn .setPadding (px , px , px , px );
154
+ rightAuxBtn .setPadding (px , px , px , px );
146
155
}
147
156
148
157
/**
149
- * Sets number keys text size .
158
+ * Sets number keys background .
150
159
*/
151
- public void setNumberKeyTextSize ( @ Dimension int size ) {
160
+ public void setNumberKeyBackground ( @ DrawableRes int background ) {
152
161
for (TextView key : numericKeys ) {
153
- key .setTextSize ( TypedValue . COMPLEX_UNIT_PX , size );
162
+ key .setBackground ( ContextCompat . getDrawable ( getContext (), background ) );
154
163
}
155
164
}
156
165
@@ -213,16 +222,14 @@ private void initializeAttributes(AttributeSet attrs) {
213
222
throw new IllegalArgumentException ("keyboardType attribute is required." );
214
223
}
215
224
// Get key sizes
216
- keyWidth = array .getLayoutDimension (R .styleable .NumberKeyboard_keyWidth ,
217
- dpToPx (DEFAULT_KEY_WIDTH_DP ));
218
- keyHeight = array .getLayoutDimension (R .styleable .NumberKeyboard_keyHeight ,
219
- dpToPx (DEFAULT_KEY_HEIGHT_DP ));
225
+ keyWidth = array .getLayoutDimension (R .styleable .NumberKeyboard_keyWidth , DEFAULT_KEY_WIDTH_DP );
226
+ keyHeight = array .getLayoutDimension (R .styleable .NumberKeyboard_keyHeight , DEFAULT_KEY_HEIGHT_DP );
227
+ // Get key padding
228
+ keyPadding = array .getDimensionPixelSize (R .styleable .NumberKeyboard_keyPadding ,
229
+ dpToPx (DEFAULT_KEY_PADDING_DP ));
220
230
// Get number key background
221
231
numberKeyBackground = array .getResourceId (R .styleable .NumberKeyboard_numberKeyBackground ,
222
232
R .drawable .key_bg );
223
- // Get number key text size
224
- numberKeyTextSize = array .getDimensionPixelSize (R .styleable .NumberKeyboard_numberKeyTextSize ,
225
- spToPx (DEFAULT_KEY_TEXT_SIZE_SP ));
226
233
// Get number key text color
227
234
numberKeyTextColor = array .getResourceId (R .styleable .NumberKeyboard_numberKeyTextColor ,
228
235
R .drawable .key_text_color );
@@ -299,8 +306,8 @@ private void inflateView() {
299
306
private void setStyles () {
300
307
setKeyWidth (keyWidth );
301
308
setKeyHeight (keyHeight );
309
+ setKeyPadding (keyPadding );
302
310
setNumberKeyBackground (numberKeyBackground );
303
- setNumberKeyTextSize (numberKeyTextSize );
304
311
setNumberKeyTextColor (numberKeyTextColor );
305
312
setLeftAuxButtonIcon (leftAuxBtnIcon );
306
313
setLeftAuxButtonBackground (leftAuxBtnBackground );
@@ -350,11 +357,4 @@ public void onClick(View v) {
350
357
public int dpToPx (float valueInDp ) {
351
358
return (int ) TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , valueInDp , getResources ().getDisplayMetrics ());
352
359
}
353
-
354
- /**
355
- * Utility method to convert sp to pixels.
356
- */
357
- public int spToPx (float valueInSp ) {
358
- return (int ) TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_SP , valueInSp , getResources ().getDisplayMetrics ());
359
- }
360
360
}
0 commit comments