From c123dd5d42f1445a2d50196e13580a3f88bfd36b Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Sun, 30 Jun 2024 17:18:53 +0200 Subject: [PATCH] Allow store name thumbnails to span multiple lines Allowing spanning multiple lines helps in case the store name doesn't fit on a single line. However, the store name should fit a single line if possible because it looks better when the store name isn't split among lines. Due to limitations in Android's autoSizeTextType system, this needs a fairly ugly workaround. --- CHANGELOG.md | 4 ++++ .../main/java/protect/card_locker/Utils.java | 17 +++++++++++++++++ app/src/main/res/layout/loyalty_card_layout.xml | 6 +----- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0d47aec5..55505758b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased - 137 + +- Allow long store names in preview to split over multiple lines + ## v2.30.0 - 136 (2024-06-18) - Support for creating a card when sharing plain text diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index eb3ef6bf1..dbba4e539 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -39,6 +39,7 @@ import androidx.appcompat.app.AppCompatDelegate; import androidx.core.graphics.ColorUtils; import androidx.core.os.LocaleListCompat; import androidx.core.view.WindowInsetsControllerCompat; +import androidx.core.widget.TextViewCompat; import androidx.exifinterface.media.ExifInterface; import androidx.palette.graphics.Palette; @@ -941,6 +942,22 @@ public class Utils { if (icon != null) { textWhenNoImage.setVisibility(View.GONE); } else { + // Manually calculate how many lines will be needed + // This is necessary because Android's auto sizing will split over lines way before reaching the minimum font size and store names split over multiple lines are harder to scan with a quick glance so we should try to prevent it + // Because we have to write the text before we can actually know the exact laid out size (trying to delay this causes bugs where the autosize fails) we have to take some... weird shortcuts + + // At this point textWhenNoImage.getWidth() still returns 0, so we cheat by calculating the whole width of the screen and then dividing it by the amount of columns + int textviewWidth = Resources.getSystem().getDisplayMetrics().widthPixels / context.getResources().getInteger(R.integer.main_view_card_columns); + + // Calculate how wide a character is and calculate how many characters fit in a line + int characterWidth = TextViewCompat.getAutoSizeMinTextSize(textWhenNoImage); + int maxWidthPerLine = textviewWidth - textWhenNoImage.getPaddingStart() - textWhenNoImage.getPaddingEnd(); + + // Set amount of lines based on what could fit at most + int maxLines = ((loyaltyCard.store.length() * characterWidth) / maxWidthPerLine) + 1; + textWhenNoImage.setMaxLines(maxLines); + + // Actually set the text and colour textWhenNoImage.setVisibility(View.VISIBLE); textWhenNoImage.setText(loyaltyCard.store); textWhenNoImage.setTextColor(Utils.needsDarkForeground(headerColor) ? Color.BLACK : Color.WHITE); diff --git a/app/src/main/res/layout/loyalty_card_layout.xml b/app/src/main/res/layout/loyalty_card_layout.xml index 45b9e158f..5daf46c65 100644 --- a/app/src/main/res/layout/loyalty_card_layout.xml +++ b/app/src/main/res/layout/loyalty_card_layout.xml @@ -42,12 +42,8 @@ android:layout_height="match_parent" android:textStyle="bold" app:autoSizeTextType="uniform" - app:autoSizeMinTextSize="12sp" - app:autoSizeMaxTextSize="100sp" - app:autoSizeStepGranularity="2sp" android:gravity="center" - android:maxLines="1" - android:layout_margin="20dp" /> + android:padding="20dp" />