2727@ SuppressWarnings ("unused" )
2828public class NumberKeyboard extends GridLayout {
2929
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 ;
3333
3434 static {
3535 AppCompatDelegate .setCompatVectorFromResourcesEnabled (true );
@@ -39,10 +39,10 @@ public class NumberKeyboard extends GridLayout {
3939 private int keyWidth ;
4040 @ Dimension
4141 private int keyHeight ;
42+ @ Dimension
43+ private int keyPadding ;
4244 @ DrawableRes
4345 private int numberKeyBackground ;
44- @ Dimension
45- private int numberKeyTextSize ;
4646 @ ColorRes
4747 private int numberKeyTextColor ;
4848 @ DrawableRes
@@ -116,6 +116,9 @@ public void showRightAuxButton() {
116116 * Sets key width in px.
117117 */
118118 public void setKeyWidth (int px ) {
119+ if (px == DEFAULT_KEY_WIDTH_DP ) {
120+ return ;
121+ }
119122 for (TextView key : numericKeys ) {
120123 key .getLayoutParams ().width = px ;
121124 }
@@ -128,6 +131,9 @@ public void setKeyWidth(int px) {
128131 * Sets key height in px.
129132 */
130133 public void setKeyHeight (int px ) {
134+ if (px == DEFAULT_KEY_HEIGHT_DP ) {
135+ return ;
136+ }
131137 for (TextView key : numericKeys ) {
132138 key .getLayoutParams ().height = px ;
133139 }
@@ -137,20 +143,23 @@ public void setKeyHeight(int px) {
137143 }
138144
139145 /**
140- * Sets number keys background .
146+ * Sets key padding in px .
141147 */
142- public void setNumberKeyBackground ( @ DrawableRes int background ) {
148+ public void setKeyPadding ( int px ) {
143149 for (TextView key : numericKeys ) {
144- key .setBackground (ContextCompat .getDrawable (getContext (), background ));
150+ key .setPadding (px , px , px , px );
151+ key .setCompoundDrawablePadding (-1 * px );
145152 }
153+ leftAuxBtn .setPadding (px , px , px , px );
154+ rightAuxBtn .setPadding (px , px , px , px );
146155 }
147156
148157 /**
149- * Sets number keys text size .
158+ * Sets number keys background .
150159 */
151- public void setNumberKeyTextSize ( @ Dimension int size ) {
160+ public void setNumberKeyBackground ( @ DrawableRes int background ) {
152161 for (TextView key : numericKeys ) {
153- key .setTextSize ( TypedValue . COMPLEX_UNIT_PX , size );
162+ key .setBackground ( ContextCompat . getDrawable ( getContext (), background ) );
154163 }
155164 }
156165
@@ -213,16 +222,14 @@ private void initializeAttributes(AttributeSet attrs) {
213222 throw new IllegalArgumentException ("keyboardType attribute is required." );
214223 }
215224 // 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 ));
220230 // Get number key background
221231 numberKeyBackground = array .getResourceId (R .styleable .NumberKeyboard_numberKeyBackground ,
222232 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 ));
226233 // Get number key text color
227234 numberKeyTextColor = array .getResourceId (R .styleable .NumberKeyboard_numberKeyTextColor ,
228235 R .drawable .key_text_color );
@@ -299,8 +306,8 @@ private void inflateView() {
299306 private void setStyles () {
300307 setKeyWidth (keyWidth );
301308 setKeyHeight (keyHeight );
309+ setKeyPadding (keyPadding );
302310 setNumberKeyBackground (numberKeyBackground );
303- setNumberKeyTextSize (numberKeyTextSize );
304311 setNumberKeyTextColor (numberKeyTextColor );
305312 setLeftAuxButtonIcon (leftAuxBtnIcon );
306313 setLeftAuxButtonBackground (leftAuxBtnBackground );
@@ -350,11 +357,4 @@ public void onClick(View v) {
350357 public int dpToPx (float valueInDp ) {
351358 return (int ) TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , valueInDp , getResources ().getDisplayMetrics ());
352359 }
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- }
360360}
0 commit comments