Skip to content

Commit e608c6e

Browse files
committed
Trim unit labels when necessary
1 parent 5403b6b commit e608c6e

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/views/NumberButtonView.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,19 @@ class NumberButtonView(
189189
textSize = dim(R.dimen.smallTextSize)
190190
typeface = getFontAwesome()
191191
}
192+
192193
value >= 0 -> {
193194
label = value.toShortString()
194195
typeface = BOLD_TYPEFACE
195196
textSize = dim(R.dimen.smallTextSize)
196197
}
198+
197199
preferences.areQuestionMarksEnabled -> {
198200
label = resources.getString(R.string.fa_question)
199201
typeface = getFontAwesome()
200202
textSize = dim(R.dimen.smallerTextSize)
201203
}
204+
202205
else -> {
203206
label = "0"
204207
typeface = BOLD_TYPEFACE
@@ -212,14 +215,23 @@ class NumberButtonView(
212215
pUnit.color = activeColor
213216

214217
if (units.isBlank()) {
218+
// Draw number without units
215219
rect.set(0f, 0f, width.toFloat(), height.toFloat())
216220
rect.offset(0f, 0.5f * em)
217221
canvas.drawText(label, rect.centerX(), rect.centerY(), pNumber)
218222
} else {
223+
// Draw number
219224
rect.set(0f, 0f, width.toFloat(), height.toFloat())
220225
canvas.drawText(label, rect.centerX(), rect.centerY(), pNumber)
226+
227+
// Draw units
228+
val maxUnitsWidth = width * 0.9f
229+
var trimmedUnits = units
230+
while (trimmedUnits.length > 2 && pUnit.measureText(trimmedUnits) > maxUnitsWidth) {
231+
trimmedUnits = trimmedUnits.dropLast(2) + ""
232+
}
221233
rect.offset(0f, 1.3f * em)
222-
canvas.drawText(units, rect.centerX(), rect.centerY(), pUnit)
234+
canvas.drawText(trimmedUnits, rect.centerX(), rect.centerY(), pUnit)
223235
}
224236

225237
drawNotesIndicator(canvas, color, em, notes)

uhabits-android/src/main/res/layout/show_habit_subtitle.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<!--
1+
<?xml version="1.0" encoding="utf-8"?><!--
32
~ Copyright (C) 2016-2021 Álinson Santos Xavier <[email protected]>
43
~
54
~ This file is part of Loop Habit Tracker.
@@ -32,8 +31,7 @@
3231
android:layout_marginBottom="8dp"
3332
android:textColor="?attr/contrast60"
3433
android:textSize="@dimen/regularTextSize"
35-
tools:text="Have you worked out today?"
36-
/>
34+
tools:text="Have you worked out today?" />
3735

3836
<LinearLayout
3937
android:layout_width="match_parent"
@@ -54,12 +52,14 @@
5452
android:id="@+id/targetText"
5553
android:layout_width="wrap_content"
5654
android:layout_height="wrap_content"
57-
android:text="8.5k"
55+
android:text=""
5856
android:textColor="?attr/contrast60"
5957
android:textSize="@dimen/smallTextSize"
6058
android:layout_marginStart="4dp"
6159
android:layout_marginEnd="16dp"
62-
/>
60+
android:maxEms="7"
61+
android:maxLines="1"
62+
android:ellipsize="end" />
6363

6464
<TextView
6565
android:id="@+id/frequencyIcon"
@@ -93,7 +93,7 @@
9393
android:layout_height="wrap_content"
9494
android:paddingTop="1dp"
9595
android:textColor="?attr/contrast60"
96-
android:text="8:00 AM"
96+
android:text=""
9797
android:layout_marginStart="4dp"
9898
android:textSize="@dimen/smallTextSize" />
9999

0 commit comments

Comments
 (0)