Cropping detection in android TextView

I have a TextView in my Android app that has a set width. Currently, it has received the gravity "center_horitonzal" and the textSize (9sp) set. I am pulling values ​​to put this label from the sqlite database, and some of the values ​​are too large to be inserted into the TextView in the current text format.

Is there any way to detect that the text inside the TextView will be clipped? I would like to detect this and omit the font until it fits. UILabel iPhone has a nice feature that handles this called "adjustToFit" (which also has a minimal font size), and I'm basically trying to emulate this.

Here is a sample TextView I'm working with:

<TextView android:id="@+id/label5" android:layout_width="62px" android:layout_height="wrap_content" android:layout_x="257px" android:layout_y="169px" android:textSize="9sp" android:textColor="#000" android:typeface="sans" android:textStyle="bold" android:gravity="center_horizontal" android:lines="1" /> 
+6
android
source share
3 answers

I found a way to measure the width of the text using the TextView Paint object and lower it until it matches the size I need. Here is a sample code:

  float size = label.getPaint().measureText(item.getTitle()); while (size > 62) { float newSize = label.getTextSize() - 0.5f; label.setTextSize(newSize); size = label.getPaint().measureText(item.getTitle()); } 
+5
source share

Another way to do this (which may be equivalent) is something like this:

  TextView title = new TextView(context) { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int textsize = 30; setTextSize(textsize); super.onMeasure(widthMeasureSpec, heightMeasureSpec); while (getMeasuredHeight() > MeasureSpec.getSize(heightMeasureSpec)) { textsize--; setTextSize(textsize); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } }; 

Just a warning that this seems to work, but I just threw it together - maybe some cases of edges with which to measure a TextView.

0
source share

I wrote this function to trim letters from the end of the text until it meets a specific width requirement.

The 0.38 function sets the proportions of the screen that I want to fill with this text, in this case it was 38%, since I wanted it to cover ~ 40%, including the addition. Worked on cases that I tested with.

Using this code to call the function below

 DisplayMetrics metrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); double maxWidth = (double)metrics.widthPixels*0.38 - 1; TextPaint painter = station.getPaint(); String left = item.getLocationName(); left = trimToLength(painter, left, maxWidth); textView.setText(left); 

This is a function.

 public String trimToLength(TextPaint painter, String initialText, double width) { String message = initialText; String output = initialText; float currentWidth = painter.measureText(output); while (currentWidth > width) { message = message.substring(0, message.length()-1); output = message + "..."; currentWidth = painter.measureText(output); } return output; } 
0
source share

All Articles