Skip to content

Commit

Permalink
Key Text Size set to accept number not number with sp or dp
Browse files Browse the repository at this point in the history
  • Loading branch information
Karikari committed Jul 2, 2021
1 parent 6026ec3 commit 34680d2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
26 changes: 24 additions & 2 deletions app/src/main/java/com/karikari/goodpinkeypad/GoodPinKeyPad.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import java.util.*


class GoodPinKeyPad : LinearLayout {
companion object {
private val TAG = GoodPinKeyPad::class.java.simpleName
Expand Down Expand Up @@ -197,7 +198,7 @@ class GoodPinKeyPad : LinearLayout {
errorIndicator = typedArray.getDrawable(R.styleable.GoodPinKeyPad_errorIndicator)

keyTextSize =
typedArray.getDimension(R.styleable.GoodPinKeyPad_keyTextSize, dpToPx(0f).toFloat())
typedArray.getFloat(R.styleable.GoodPinKeyPad_keyTextSize, 0f)

errorTextSize =
typedArray.getDimension(
Expand Down Expand Up @@ -225,7 +226,7 @@ class GoodPinKeyPad : LinearLayout {
}

if (keyTextSize != 0f) {
textView.textSize = keyTextSize
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, keyTextSize)
}
}
if (keypadBackPressIcon != null) {
Expand Down Expand Up @@ -475,4 +476,25 @@ class GoodPinKeyPad : LinearLayout {
return TypedValue.applyDimension(1, valueInDp, this.resources.displayMetrics)
.toInt()
}

fun dpToSp(dp: Float, context: Context): Int {
return (dpToPxx(dp, context) / context.resources.displayMetrics.scaledDensity).toInt()
}
fun dpToPxx(dp: Float, context: Context): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
context.resources.displayMetrics
)
.toInt()
}

fun spToPx(sp: Float, context: Context): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
sp,
context.resources.displayMetrics
)
.toInt()
}
}
11 changes: 5 additions & 6 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/eeirie_black_transparent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@color/eeirie_black_transparent"
tools:showIn="@layout/activity_main">


Expand All @@ -22,11 +21,11 @@
app:keyBackground="@drawable/keys_white_theme_selector"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:marginTop="5dp"
app:marginTop="10dp"
app:pinEntry="four"
app:keyTextSize="30"
app:selectedIndicator="@drawable/black_solid_dot"
app:textColor="#db3a34"
tools:layout_editor_absoluteY="100dp" />


</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<attr name="font" format="reference"/>

<attr name="keyTextSize" format="dimension"/>
<attr name="keyTextSize" format="float"/>

<attr name="errorTextSize" format="dimension"/>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>
<dimen name="fab_margin">16dp</dimen>
<dimen name="key_size">60dp</dimen>
<dimen name="key_text_size">30sp</dimen>
<dimen name="key_text_size">35sp</dimen>
<dimen name="key_margin">50dp</dimen>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class GoodPinKeyPad : LinearLayout {
errorIndicator = typedArray.getDrawable(R.styleable.GoodPinKeyPad_errorIndicator)

keyTextSize =
typedArray.getDimension(R.styleable.GoodPinKeyPad_keyTextSize, dpToPx(0f).toFloat())
typedArray.getFloat(R.styleable.GoodPinKeyPad_keyTextSize, 0f)

errorTextSize =
typedArray.getDimension(
Expand Down Expand Up @@ -225,7 +225,7 @@ class GoodPinKeyPad : LinearLayout {
}

if (keyTextSize != 0f) {
textView.textSize = keyTextSize
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, keyTextSize)
}
}
if (keypadBackPressIcon != null) {
Expand Down
2 changes: 1 addition & 1 deletion goodpinkeypad/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<attr name="font" format="reference"/>

<attr name="keyTextSize" format="dimension"/>
<attr name="keyTextSize" format="float"/>

<attr name="errorTextSize" format="dimension"/>

Expand Down

0 comments on commit 34680d2

Please sign in to comment.