Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autosizing TextViews in RecyclerView causes text size to decrease

I am trying to use Autosizing TextViews in a RecyclerView, but when I scroll a few times the text gets so small that it's obviously not working properly.

Example of my TextView:

<android.support.v7.widget.AppCompatTextView
        android:id="@+id/textview_unit_title"
        android:layout_width="@dimen/tile_image_size"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:textSize="@dimen/medium_size"
        android:textColor="@color/color_text"
        android:paddingTop="@dimen/padding_title"
        android:layout_marginRight="2dp"
        android:layout_marginEnd="2dp"
        app:autoSizeMaxTextSize="@dimen/style_medium"
        app:autoSizeTextType="uniform"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/imageview_unit_icon"
        app:layout_constraintTop_toTopOf="parent"/>

Should I update this scaling somewhere else programmatically or is there another solution?

like image 770
HyperX Avatar asked Jun 05 '18 09:06

HyperX


People also ask

What attribute changes the size of TextView?

To define a range of text sizes and a dimension in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.

Should Use SP instead of DP for text sizes?

An sp is the same base unit (as dp), but is scaled by the user's preferred text size (it's a scale-independent pixel), so you should use this measurement unit when defining text size (but never for layout sizes).

How do I change font size in android XML?

Adding fonts to a TextView To set a font for the TextView , do one of the following: In the layout XML file, set the fontFamily attribute to the font file you want to access. Open the Properties window to set the font for the TextView .

How do I change font size in android programmatically?

To set Android Button font/text size, we can set android:textSize attribute for Button in layout XML file. To programmatically set or change Android Button font/text size, we can pass specified size to the method Button. setTextSize(specific_size).


1 Answers

The issue I've seen with this is that setting your view height to be wrap_content allows the text size to get smaller, but the text will never get bigger again. This is why the documentation recommends to not use wrap_content for the view size. However, I've found that if you turn off the auto-resizing, set the text size to whatever the max is, then re-enable auto-resizing, the text size resets to the largest size and scales down as necessary.

So my view in XML would look like:

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/text_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:textAllCaps="true"
    android:textColor="@android:color/white"
    android:textSize="42sp"
    app:autoSizeMinTextSize="26dp"
    app:autoSizeMaxTextSize="42dp"
    app:autoSizeTextType="none"/>

Then in my ViewHolder when I bind my text to the view:

TextView title = view.findViewById(R.id.text_title);
String titleValue = "Some Title Value";

// Turn off auto-sizing text.
TextViewCompat.setAutoSizeTextTypeWithDefaults(title, 
    TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);

// Bump text size back up to the max value.
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 42);

// Set your text as normal.
title.setText(titleValue);

// Post a runnable to re-enable auto-sizing text so that it occurs
// after the view is laid out and measured at max text size.
title.post(new Runnable() {
    @Override
    public void run() {     
        TextViewCompat
            .setAutoSizeTextTypeUniformWithConfiguration(title,
                    26, 42, 1, TypedValue.COMPLEX_UNIT_DIP);
    }
});
like image 59
Michael Celey Avatar answered Sep 23 '22 08:09

Michael Celey