Skip to content

Commit e9568d9

Browse files
authored
Merge pull request #144 from ismailnurudeen/feat/key_seconday_icon
Feat/added secondary icon to key
2 parents eaeef9b + 6a46af1 commit e9568d9

18 files changed

+79
-13
lines changed

app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/MyKeyboard.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class MyKeyboard {
138138
/** Icon to display instead of a label. Icon takes precedence over a label */
139139
var icon: Drawable? = null
140140

141+
/** Icon to display top left of an icon.*/
142+
var secondaryIcon: Drawable? = null
143+
141144
/** Width of the key, not including the gap */
142145
var width: Int
143146

@@ -204,6 +207,9 @@ class MyKeyboard {
204207
icon = a.getDrawable(R.styleable.MyKeyboard_Key_keyIcon)
205208
icon?.setBounds(0, 0, icon!!.intrinsicWidth, icon!!.intrinsicHeight)
206209

210+
secondaryIcon = a.getDrawable(R.styleable.MyKeyboard_Key_secondaryKeyIcon)
211+
secondaryIcon?.setBounds(0, 0, secondaryIcon!!.intrinsicWidth, secondaryIcon!!.intrinsicHeight)
212+
207213
label = a.getText(R.styleable.MyKeyboard_Key_keyLabel) ?: ""
208214
topSmallNumber = a.getString(R.styleable.MyKeyboard_Key_topSmallNumber) ?: ""
209215

@@ -230,10 +236,12 @@ class MyKeyboard {
230236
fun isInside(x: Int, y: Int): Boolean {
231237
val leftEdge = edgeFlags and EDGE_LEFT > 0
232238
val rightEdge = edgeFlags and EDGE_RIGHT > 0
233-
return ((x >= this.x || leftEdge && x <= this.x + width)
234-
&& (x < this.x + width || rightEdge && x >= this.x)
235-
&& (y >= this.y && y <= this.y + height)
236-
&& (y < this.y + height && y >= this.y))
239+
return (
240+
(x >= this.x || leftEdge && x <= this.x + width) &&
241+
(x < this.x + width || rightEdge && x >= this.x) &&
242+
(y >= this.y && y <= this.y + height) &&
243+
(y < this.y + height && y >= this.y)
244+
)
237245
}
238246
}
239247

@@ -249,7 +257,7 @@ class MyKeyboard {
249257
mDefaultHorizontalGap = 0
250258
mDefaultWidth = mDisplayWidth / 10
251259
mDefaultHeight = mDefaultWidth
252-
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier);
260+
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier)
253261
mKeys = ArrayList()
254262
mEnterKeyType = enterKeyType
255263
loadKeyboard(context, context.resources.getXml(xmlLayoutResId))
@@ -273,7 +281,7 @@ class MyKeyboard {
273281
row.defaultHeight = mDefaultHeight
274282
row.defaultWidth = keyWidth
275283
row.defaultHorizontalGap = mDefaultHorizontalGap
276-
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier);
284+
mKeyboardHeightMultiplier = getKeyboardHeightMultiplier(context.config.keyboardHeightMultiplier)
277285

278286
characters.forEachIndexed { index, character ->
279287
val key = Key(row)
@@ -386,7 +394,7 @@ class MyKeyboard {
386394
}
387395

388396
private fun getKeyboardHeightMultiplier(multiplierType: Int): Float {
389-
return when(multiplierType) {
397+
return when (multiplierType) {
390398
KEYBOARD_HEIGHT_MULTIPLIER_SMALL -> 1.0F
391399
KEYBOARD_HEIGHT_MULTIPLIER_MEDIUM -> 1.2F
392400
KEYBOARD_HEIGHT_MULTIPLIER_LARGE -> 1.4F

app/src/main/kotlin/com/simplemobiletools/keyboard/views/MyKeyboardView.kt

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -609,16 +609,53 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
609609

610610
if (code == KEYCODE_ENTER) {
611611
key.icon!!.applyColorFilter(mPrimaryColor.getContrastColor())
612+
key.secondaryIcon?.applyColorFilter(mPrimaryColor.getContrastColor())
612613
} else if (code == KEYCODE_DELETE || code == KEYCODE_SHIFT || code == KEYCODE_EMOJI) {
613614
key.icon!!.applyColorFilter(mTextColor)
615+
key.secondaryIcon?.applyColorFilter(mTextColor)
614616
}
617+
val keyIcon = key.icon!!
618+
val secondaryIcon = key.secondaryIcon
615619

616-
val drawableX = (key.width - key.icon!!.intrinsicWidth) / 2
617-
val drawableY = (key.height - key.icon!!.intrinsicHeight) / 2
618-
canvas.translate(drawableX.toFloat(), drawableY.toFloat())
619-
key.icon!!.setBounds(0, 0, key.icon!!.intrinsicWidth, key.icon!!.intrinsicHeight)
620-
key.icon!!.draw(canvas)
621-
canvas.translate(-drawableX.toFloat(), -drawableY.toFloat())
620+
if (secondaryIcon != null) {
621+
val keyIconWidth = (keyIcon.intrinsicWidth * 0.9f).toInt()
622+
val keyIconHeight = (keyIcon.intrinsicHeight * 0.9f).toInt()
623+
val secondaryIconWidth = (secondaryIcon.intrinsicWidth * .6f).toInt()
624+
val secondaryIconHeight = (secondaryIcon.intrinsicHeight * .6f).toInt()
625+
626+
val centerX = key.width / 2
627+
val centerY = key.height / 2
628+
629+
val keyIconLeft = centerX - keyIconWidth / 2
630+
val keyIconTop = centerY - keyIconHeight / 2
631+
632+
keyIcon.setBounds(keyIconLeft, keyIconTop, keyIconLeft + keyIconWidth, keyIconTop + keyIconHeight)
633+
keyIcon.draw(canvas)
634+
635+
val secondaryIconPaddingRight = 10
636+
val secondaryIconLeft = key.width - secondaryIconPaddingRight - secondaryIconWidth
637+
val secondaryIconRight = secondaryIconLeft + secondaryIconWidth
638+
639+
val secondaryIconTop = 14 // This will act as a topPadding
640+
val secondaryIconBottom = secondaryIconTop + secondaryIconHeight
641+
642+
secondaryIcon.setBounds(
643+
secondaryIconLeft,
644+
secondaryIconTop,
645+
secondaryIconRight,
646+
secondaryIconBottom
647+
)
648+
secondaryIcon.draw(canvas)
649+
650+
secondaryIcon.draw(canvas)
651+
} else {
652+
val drawableX = (key.width - keyIcon.intrinsicWidth) / 2
653+
val drawableY = (key.height - keyIcon.intrinsicHeight) / 2
654+
canvas.translate(drawableX.toFloat(), drawableY.toFloat())
655+
keyIcon.setBounds(0, 0, keyIcon.intrinsicWidth, keyIcon.intrinsicHeight)
656+
keyIcon.draw(canvas)
657+
canvas.translate(-drawableX.toFloat(), -drawableY.toFloat())
658+
}
622659
}
623660
canvas.translate(-key.x.toFloat(), -key.y.toFloat())
624661
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#ffffff"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM18.92,8h-2.95c-0.32,-1.25 -0.78,-2.45 -1.38,-3.56 1.84,0.63 3.37,1.91 4.33,3.56zM12,4.04c0.83,1.2 1.48,2.53 1.91,3.96h-3.82c0.43,-1.43 1.08,-2.76 1.91,-3.96zM4.26,14C4.1,13.36 4,12.69 4,12s0.1,-1.36 0.26,-2h3.38c-0.08,0.66 -0.14,1.32 -0.14,2s0.06,1.34 0.14,2L4.26,14zM5.08,16h2.95c0.32,1.25 0.78,2.45 1.38,3.56 -1.84,-0.63 -3.37,-1.9 -4.33,-3.56zM8.03,8L5.08,8c0.96,-1.66 2.49,-2.93 4.33,-3.56C8.81,5.55 8.35,6.75 8.03,8zM12,19.96c-0.83,-1.2 -1.48,-2.53 -1.91,-3.96h3.82c-0.43,1.43 -1.08,2.76 -1.91,3.96zM14.34,14L9.66,14c-0.09,-0.66 -0.16,-1.32 -0.16,-2s0.07,-1.35 0.16,-2h4.68c0.09,0.65 0.16,1.32 0.16,2s-0.07,1.34 -0.16,2zM14.59,19.56c0.6,-1.11 1.06,-2.31 1.38,-3.56h2.95c-0.96,1.65 -2.49,2.93 -4.33,3.56zM16.36,14c0.08,-0.66 0.14,-1.32 0.14,-2s-0.06,-1.34 -0.14,-2h3.38c0.16,0.64 0.26,1.31 0.26,2s-0.1,1.36 -0.26,2h-3.38z"/>
5+
</vector>

app/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
<attr name="keyLabel" format="string" />
3434
<!-- The icon to display on the key instead of the label. -->
3535
<attr name="keyIcon" format="reference" />
36+
<!-- The icon to display on the top left of a key with icon. -->
37+
<attr name="secondaryKeyIcon" format="reference" />
3638
<!-- Top small number shown above letters of the first row. -->
3739
<attr name="topSmallNumber" format="string" />
3840
</declare-styleable>

app/src/main/res/xml/keys_letters_bengali.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
app:code="-6"
163163
app:keyEdgeFlags="left"
164164
app:keyIcon="@drawable/ic_emoji_emotions_outline_vector"
165+
app:secondaryKeyIcon="@drawable/ic_language_outlined"
165166
app:keyWidth="10%p" />
166167
<Key
167168
app:code="32"

app/src/main/res/xml/keys_letters_bulgarian.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
app:code="-6"
124124
app:keyEdgeFlags="left"
125125
app:keyIcon="@drawable/ic_emoji_emotions_outline_vector"
126+
app:secondaryKeyIcon="@drawable/ic_language_outlined"
126127
app:keyWidth="10%p" />
127128
<Key
128129
app:code="32"

app/src/main/res/xml/keys_letters_english_dvorak.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
app:code="-6"
127127
app:keyEdgeFlags="left"
128128
app:keyIcon="@drawable/ic_emoji_emotions_outline_vector"
129+
app:secondaryKeyIcon="@drawable/ic_language_outlined"
129130
app:keyWidth="10%p" />
130131
<Key
131132
app:code="32"

app/src/main/res/xml/keys_letters_english_qwerty.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
app:code="-6"
126126
app:keyEdgeFlags="left"
127127
app:keyIcon="@drawable/ic_emoji_emotions_outline_vector"
128+
app:secondaryKeyIcon="@drawable/ic_language_outlined"
128129
app:keyWidth="10%p" />
129130
<Key
130131
app:code="32"

app/src/main/res/xml/keys_letters_english_qwertz.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
app:code="-6"
126126
app:keyEdgeFlags="left"
127127
app:keyIcon="@drawable/ic_emoji_emotions_outline_vector"
128+
app:secondaryKeyIcon="@drawable/ic_language_outlined"
128129
app:keyWidth="10%p" />
129130
<Key
130131
app:code="32"

app/src/main/res/xml/keys_letters_french.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
app:code="-6"
107107
app:keyEdgeFlags="left"
108108
app:keyIcon="@drawable/ic_emoji_emotions_outline_vector"
109+
app:secondaryKeyIcon="@drawable/ic_language_outlined"
109110
app:keyWidth="10%p" />
110111
<Key
111112
app:code="32"

0 commit comments

Comments
 (0)