How much text can be set in a TextView so that it does not need to scroll

How much text can be set in text form for a specific text string with a specific font and size so that the text image does not need to be scrolled. I mean, how much text can be placed inside a TextView without having to scroll. This is similar to How to determine how much text will fit into a TextView in Android? but I can not find a working solution. Please, help.

+4
source share
4 answers

Assuming you have already looked for other options, and there is no easy way to do this, here is a theoretical (I have not tested it) hacker approach that might work.

Create a new class that extends TextView. Then override the method that will be called after updating the contents of the TextView. There may be a better way to use it, but in this example, try onDraw (). This method will check the width and see if it needs to be scrolled. If so, it truncates the line and sets the text. He will do this in a loop until he needs to scroll more.

@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(getWidth() < computeHorizontalScrollRange()){ // Requires scrolling, the string is too long // Do whatever you need to do to trim the string, you could also grab the remaining string and do something with it. // Set the TextView to use the trimmed string. } } 

You want to make sure that it does not fall into an infinite loop, and also check if the width is equal.

You can also look at various android:ellipsize options android:ellipsize .

+1
source

I really needed something like that. I needed the width of the TextView half-fixed size, but the text always needed it. Since size doesn't matter much, I created a view that resizes the text to fit it.

 import android.content.Context; import android.util.AttributeSet; import android.util.TypedValue; import android.widget.TextView; /** * The text inside this TextView will resize to fit. * It will resize until it fits within the view and padding */ public class FitTextView extends TextView { private float defaultSize = 12; public FitTextView(Context context) { super(context); } public FitTextView(Context context, AttributeSet attrs) { super(context, attrs); defaultSize = getTextSize(); } public FitTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); defaultSize = getTextSize(); } /** * Set the default size. This size is set every time the * view is resized. * @param size */ public void setDefaultSize(float size) { defaultSize = size; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { setTextSize(TypedValue.COMPLEX_UNIT_PX, defaultSize); fitCharsInView(); super.onSizeChanged(w, h, oldw, oldh); } @Override public void setText(CharSequence text, BufferType type) { fitCharsInView(); super.setText(text, type); } /** * Decreases the text size until it fits inside the view */ public void fitCharsInView() { int padding = getPaddingLeft() + getPaddingRight(); int viewWidth = getWidth() - padding; float textWidth = getTextWidth(); int iKillInfite = 0; int maxIteration = 10000; while(textWidth > viewWidth && iKillInfite < maxIteration) { iKillInfite++; float textSize = getTextSize(); setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize-1); textWidth = getTextWidth(); } } /** * Gets the width in pixels of the text * @return */ private float getTextWidth() { return getPaint().measureText(getText().toString()); } } 
+1
source

This is just a quick brainstorm:

  • Character length measurement
  • Get information about screen size and density.
  • Get font size, line height
  • Calculate the approximate number of spaces this line will take, given the above data.
  • Separate the line accordingly
0
source

I would extend the TextView class and override the onLayout() method to crop the string as long as the measured height is greater than computeHorizontalScrollRange() .

I think the solution is a little better because:

  • It only trims the text once (when the view is created);
  • You can easily save the "cropped" text if you want to restore it in the future;
  • OOP is awesome.
0
source

Source: https://habr.com/ru/post/1336841/


All Articles