Skip to content

Commit 202dbef

Browse files
committed
Use Autosizing TextViews #8
1 parent ecec3ef commit 202dbef

File tree

10 files changed

+183
-75
lines changed

10 files changed

+183
-75
lines changed

CHANGELOG.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Change Log
2+
3+
## Version 2.0 *(20/04/2018)*
4+
5+
- Implement keys using Autosizing TextViews #8
6+
- Adding `keyPadding` attribute
7+
- Remove `numberKeyTextSize` attribute **(breaking change)**
8+
- Update support libraries to 27.1.1 #7
9+
- Update buildToolsVersion to 27.0.3 #7
10+
- Update gradle to 4.6 #7
11+
12+
## Version 1.0 *(18/01/2018)*
13+
14+
- Update support libraries to 27.0.2
15+
- Update buildToolsVersion to 26.0.2
16+
- Update gradle to 4.1
17+
- Improve decimal sample
18+
19+
## Version 0.3 *(25/09/2017)*
20+
21+
- Update support libraries to 26.1.0
22+
- Update buildToolsVersion to 26.0.1
23+
24+
## Version 0.2 *(25/09/2017)*
25+
26+
- Library crashes when running on API 19 #1
27+
- Update buildToolsVersion "25.0.3"
28+
29+
## Version 0.1 *(20/07/2017)*
30+
31+
- Initial release

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ Add the dependency:
2525

2626
```gradle
2727
dependencies {
28-
compile 'com.github.davidmigloz:number-keyboard:1.0'
28+
compile 'com.github.davidmigloz:number-keyboard:2.0'
2929
}
3030
```
3131

32+
[CHANGELOG](https://github.com/davidmigloz/number-keyboard/blob/master/CHANGELOG.md)
33+
3234
#### Step 3
3335

3436
Use `NumberKeyboard` view in your layout:
@@ -49,9 +51,10 @@ Use `NumberKeyboard` view in your layout:
4951
- `decimal`: numbers, comma and backspace keys.
5052
- `fingerprint`: numbers, fingerprint and backspace keys.
5153
- `custom`: numbers and defined auxiliary keys.
52-
- `keyboard:keyHeight="[dimension]"` (default: 70dp): key height.
54+
- `keyboard:keyWidth="[dimension]"` (default: `match_parent`): key width (`wrap_content` not allowed).
55+
- `keyboard:keyHeight="[dimension]"` (default: `match_parent`): key height (`wrap_content` not allowed).
56+
- `keyboard:keyPadding="[dimension]"` (default: `16dp`): key padding.
5357
- `keyboard:numberKeyBackground="[reference]"` (default: circle): number keys background drawable.
54-
- `keyboard:numberKeyTextSize="[dimension]"` (default: 32sp): number keys text size.
5558
- `keyboard:numberKeyTextColor="[reference]"` (default: dark blue): number keys text color.
5659
- `keyboard:leftAuxBtnIcon="[reference]"` (default: none): if `keyboardType="custom"`, icon shown in left auxiliary button.
5760
- `keyboard:leftAuxBtnBackground="[reference]"` (default: none): if `keyboardType="custom"`, left auxiliary button background.
@@ -66,8 +69,8 @@ Use `NumberKeyboard` view in your layout:
6669
- `showRightAuxButton()`: shows right auxiliary button.
6770
- `setKeyWidth()`: sets key width in px.
6871
- `setKeyHeight()`: sets key height in px.
72+
- `setKeyPadding()`: sets key padding in px.
6973
- `setNumberKeyBackground()`: sets number keys background.
70-
- `setNumberKeyTextSize()`: sets number keys text size.
7174
- `setNumberKeyTextColor()`: sets number keys text color.
7275
- `setNumberKeyTypeface()`: sets number keys text typeface.
7376
- `setLeftAuxButtonIcon()`: sets left auxiliary button icon.
@@ -105,7 +108,7 @@ Pull request are very appreciated.
105108

106109
## License
107110

108-
Copyright (c) 2017 David Miguel Lozano
111+
Copyright (c) 2018 David Miguel Lozano
109112

110113
Licensed under the Apache License, Version 2.0 (the "License");
111114
you may not use this file except in compliance with the License.

img/screenshot.jpg

14.2 KB
Loading

lib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ android {
2828

2929
dependencies {
3030
implementation 'com.android.support:appcompat-v7:27.1.1'
31+
implementation 'com.android.support:support-v4:27.1.1'
3132
api "com.android.support:gridlayout-v7:27.1.1"
3233
}
3334

lib/src/main/java/com/davidmiguel/numberkeyboard/NumberKeyboard.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
@SuppressWarnings("unused")
2828
public 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
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2014 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.davidmiguel.numberkeyboard;
18+
19+
import android.content.Context;
20+
import android.util.AttributeSet;
21+
import android.widget.FrameLayout;
22+
23+
/**
24+
* {@link android.widget.FrameLayout} which forces itself to be laid out as square.
25+
* https://developer.android.com/samples/ActivitySceneTransitionBasic/src/com.example.android.activityscenetransitionbasic/SquareFrameLayout.html#l26
26+
*/
27+
public class SquareFrameLayout extends FrameLayout {
28+
29+
public SquareFrameLayout(Context context) {
30+
super(context);
31+
}
32+
33+
public SquareFrameLayout(Context context, AttributeSet attrs) {
34+
super(context, attrs);
35+
}
36+
37+
public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
38+
super(context, attrs, defStyleAttr);
39+
}
40+
41+
@Override
42+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
43+
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
44+
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
45+
46+
if (widthSize == 0 && heightSize == 0) {
47+
// If there are no constraints on size, let FrameLayout measure
48+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
49+
50+
// Now use the smallest of the measured dimensions for both dimensions
51+
final int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
52+
setMeasuredDimension(minSize, minSize);
53+
return;
54+
}
55+
56+
final int size;
57+
if (widthSize == 0 || heightSize == 0) {
58+
// If one of the dimensions has no restriction on size, set both dimensions to be the
59+
// on that does
60+
size = Math.max(widthSize, heightSize);
61+
} else {
62+
// Both dimensions have restrictions on size, set both dimensions to be the
63+
// smallest of the two
64+
size = Math.min(widthSize, heightSize);
65+
}
66+
67+
final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
68+
super.onMeasure(newMeasureSpec, newMeasureSpec);
69+
}
70+
}

0 commit comments

Comments
 (0)