0

Suppose I have arbitrary text to display and I can't put it in a ScrollView or anything similar because of the application needs and the rest of the layout details -- the text can be any length, has to be on one line, and the only way to get it all on screen if it's too large is to scale it down according to display dimensions. It needs to use wrap_content for width and height as it may wind up smaller than the display bounds and we don't want to stretch it in that case.

To address this problem, I first tried placing the TextView in a ConstraintLayout and constraining its width and height; I expected this to automatically scale the TextView down if it was too large, in order to fit the constraints of its parent whose width/height are given as match_parent. Unfortunately, it had no obvious effect and the text ran happily off screen. Next I tried to manually scale the TextView to fit from my app code, but when I call getMeasuredWidth/Height on the TextView I always get the display dimensions back (presumably because the measurement is clamping to display bounds).

Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

    <TextView
        android:id="@+id/text"
        android:text="teeeeeeeeeeeeeeesteeeeeeeeeeeeeeeeeeeeeeesteeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeesteeeeeeeeeeeeeeeeeeeest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="300sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constrainedWidth="true"
        app:layout_constrainedHeight="true"
        android:maxLines="1"
        />

</android.support.constraint.ConstraintLayout>

Measure reporting code:

    View textView = findViewById(R.id.text);
    ViewTreeObserver textViewTreeObserver = textView.getViewTreeObserver();
    textViewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Log.i("MyActivity","oncreate; text view dimens are "+textView.getMeasuredWidth()+","+textView.getMeasuredHeight());
        }
    });

The logs are telling me "text view dimens are 2094,1017" which are the display dimens minus system views, and the text is just cut off after there's too much to fit on screen (all I see on screen is "teee").

Note

The TextView example is just a symptom of the larger problem; I may need to deal with runtime images etc. that are too large for the display, so I'm looking for a way to detect and scale down (or ideally set up ConstraintLayout to do so for me) arbitrarily large runtime content set in a View. The only option that comes to mind (considering the fact that View's measurements apparently clamp to display bounds) is to figure out the problem before View gets involved, e.g. by looking at a raw image's dimens or the text data we've received for a TextView vs. the TextView's current font scaling factor and modifying as needed before setting it as the View's content. The trouble with that is it's very content type dependent, and I was hoping to avoid writing a lot of code to manage runtime content when View subclasses theoretically already have the information at some point during layout.

Questions:

  1. Why didn't the constraint solution scale the TextView down automatically to fit within the ConstraintLayout's match_parent width and height per this answer?
  2. How can I get the total size in pixels it would take to display a view's contents even if that value is greater than the actual device display boundaries?
CCJ
  • 1,619
  • 26
  • 41

1 Answers1

1
android:textSize="300sp" //Its a fixed value

Setting too large font size make the result, you could refer the autosizing-textview to learn how to handle this case.

navylover
  • 12,383
  • 5
  • 28
  • 41
  • Interesting tip, but can't ConstraintLayout scale down fixed value view dimens as necessary to meet constraints? The giant TextView is just one example of the general problem -- I'm looking for a way to detect and scale down (or ideally set up ConstraintLayout to do so for me) arbitrarily large runtime content. – CCJ Aug 26 '20 at 13:01