Padding fixes to ensure the cards scale well without changing text size in LoyaltyCardViewActivity

This commit is contained in:
Sylvia van Os
2024-11-01 22:35:33 +01:00
parent 328c110a97
commit dc65030a63
4 changed files with 7 additions and 15 deletions

View File

@@ -30,6 +30,7 @@ import java.text.DateFormat;
import java.util.ArrayList;
import protect.card_locker.databinding.LoyaltyCardLayoutBinding;
import protect.card_locker.preferences.Settings;
public class LoyaltyCardCursorAdapter extends BaseCursorAdapter<LoyaltyCardCursorAdapter.LoyaltyCardListItemViewHolder> {
private int mCurrentSelectedIndex = -1;
@@ -123,7 +124,7 @@ public class LoyaltyCardCursorAdapter extends BaseCursorAdapter<LoyaltyCardCurso
}
inputHolder.mCardIcon.setContentDescription(loyaltyCard.store);
inputHolder.mIconBackgroundColor = Utils.setIconOrTextWithBackground(mContext, loyaltyCard, icon, inputHolder.mCardIcon, inputHolder.mCardText);
inputHolder.mIconBackgroundColor = Utils.setIconOrTextWithBackground(mContext, loyaltyCard, icon, inputHolder.mCardIcon, inputHolder.mCardText, new Settings(mContext).getPreferredColumnCount());
inputHolder.toggleCardStateIcon(loyaltyCard.starStatus != 0, loyaltyCard.archiveStatus != 0, itemSelected(inputCursor.getPosition()));
@@ -339,10 +340,4 @@ public class LoyaltyCardCursorAdapter extends BaseCursorAdapter<LoyaltyCardCurso
}
}
public int dpToPx(int dp, Context mContext) {
Resources r = mContext.getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
return px;
}
}

View File

@@ -720,7 +720,7 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
binding.fabEdit.setImageDrawable(editButtonIcon);
Bitmap icon = Utils.retrieveCardImage(this, loyaltyCard.id, ImageLocationType.icon);
Utils.setIconOrTextWithBackground(this, loyaltyCard, icon, binding.iconImage, binding.iconText);
Utils.setIconOrTextWithBackground(this, loyaltyCard, icon, binding.iconImage, binding.iconText, 1);
// If the background is very bright, we should use dark icons
backgroundNeedsDarkIcons = Utils.needsDarkForeground(backgroundHeaderColor);

View File

@@ -934,7 +934,7 @@ public class Utils {
* @param textWhenNoImage TextView to write the loyalty card name into if icon is null
* @return background colour
*/
public static int setIconOrTextWithBackground(Context context, LoyaltyCard loyaltyCard, Bitmap icon, ImageView backgroundOrIcon, TextView textWhenNoImage) {
public static int setIconOrTextWithBackground(Context context, LoyaltyCard loyaltyCard, Bitmap icon, ImageView backgroundOrIcon, TextView textWhenNoImage, int columnCount) {
int headerColor = getHeaderColor(context, loyaltyCard);
backgroundOrIcon.setImageBitmap(icon);
backgroundOrIcon.setBackgroundColor(headerColor);
@@ -946,9 +946,8 @@ public class Utils {
// 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
var settings = new Settings(context);
// 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 columnWidth = Resources.getSystem().getDisplayMetrics().widthPixels / settings.getPreferredColumnCount();
int columnWidth = Resources.getSystem().getDisplayMetrics().widthPixels / columnCount;
// Calculate how wide a character is and calculate how many characters fit in a line
// text size is generally based on height, so setting 1:1 as width may be fishy
@@ -958,10 +957,7 @@ public class Utils {
// Set number of lines based on what could fit at most
int fullTextWidth = loyaltyCard.store.length() * characterWidth;
int maxLines = (fullTextWidth / maxWidthPerLine) + 1;
// default of 20dp crushes the text
var relativePadding = (int) (columnWidth * 0.1);
textWhenNoImage.setMaxLines(maxLines);
textWhenNoImage.setPadding(relativePadding, relativePadding, relativePadding, relativePadding);
// Actually set the text and colour
textWhenNoImage.setVisibility(View.VISIBLE);

View File

@@ -41,9 +41,10 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="bold"
app:autoSizeMinTextSize="6sp"
app:autoSizeTextType="uniform"
android:gravity="center"
android:padding="20dp" />
android:padding="10dp" />
<ImageView
android:importantForAccessibility="no"