1

I'm now trying to resolve an issue with somehow overlapping text in TextView. I've posted a question about that , but I've also tried to solve it myself. I decided to count the amount of text which causes the textview to break the line. I came up with this unpolished code, which basically should inflate the view, set it's layout params according to the displayed size and then run onmeasure and return lineCount. I plan than to use probably binary search to find exact text length which fits into the textview, but even before I've just tried to run the code and see how it behaves. It's kind of weird, because it gives me different results, than what I see on screen than. I even tried to alter the textsize according to scaled density, because I wasn't sure whether it's been taken into account.

Here is the code. It returns three,but when I render the layout to screen the text takes up only two lines.

public int getCountOfLines(Context context, int widgetDp){

    LayoutInflater mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout layout = (LinearLayout) mLayoutInflater.inflate(R.layout.event_basic_large ,null, false);

    TextView titleTextView = (TextView) layout.findViewById(R.id.itemTitle);
    titleTextView.setText(TEST_TEXT);

    float density = context.getResources().getDisplayMetrics().density;
    float textDensity = context.getResources().getDisplayMetrics().scaledDensity;

    titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextView.getTextSize()/density*textDensity);

    layout.setLayoutParams(
            new LinearLayout.LayoutParams(
                    (int) (widgetDp*density),
                    ViewGroup.LayoutParams.WRAP_CONTENT)
    );


    layout.measure(View.MeasureSpec.makeMeasureSpec(
            (int) (widgetDp*density), View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
    );

    Log.d(TAG, titleTextView.getLineCount() + " lines, width "+titleTextView.getMeasuredWidth());


    return titleTextView.getLineCount();
}
Community
  • 1
  • 1
simekadam
  • 7,334
  • 11
  • 56
  • 79

1 Answers1

1

I ran into this a while back and after searching and trying finally got the following function to work:

public int getLineCount(String testString, float textSize, 
        float width) {
    Rect bounds = new Rect();
    Paint paint = new Paint();
    paint.setTypeface(<font>);//set font that you are using for this
    paint.setTextSize(textSize);
    paint.getTextBounds(testString, 0, testString.length(), bounds);

    return (int) Math.ceil(bounds.width() / width);
}

This function uses textSize and width to give number of lines. This functions give number of line in a textview before it is displayed.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • Why you gives the textview reference as the parameter? Looks good,but I'm still getting crazy whyY My code does what it does:/ – simekadam Jan 16 '14 at 07:17
  • 1
    I had a use of TextView but just count line it is not required. – vipul mittal Jan 16 '14 at 07:24
  • this approach is quite goog because it gives me the exact ratio..I can then quite easily trim the text..thank you – simekadam Jan 16 '14 at 08:38
  • Well, so I've tried it and I'm still getting different results, than what I should.. https://gist.github.com/anonymous/8452487 it's the code which I have. The only difference is that the onscreen textview is actually a remoteview in appwidget, so I'm measuring it's size manually - you can see that in my original code from the question – simekadam Jan 16 '14 at 10:02
  • it must be because of px and dp values this function is expecting px values – vipul mittal Jan 16 '14 at 10:03
  • + you must pass the textSize of the text you are about to set – vipul mittal Jan 16 '14 at 10:04
  • this is how I call it: `getLineCount(TEST_TEXT, titleTextView.getMeasuredWidth(), titleTextView);` so it really shouldn't be the issue..it might be actually somewhere in the process of measuring the layout..I'm not that sure it's done right:) – simekadam Jan 16 '14 at 10:06
  • the textSize is not in the Paint? I'll try that – simekadam Jan 16 '14 at 10:08
  • I am not sure it's same as the textview. also check the value from getMeasuredWidth is correct or not? – vipul mittal Jan 16 '14 at 10:10
  • From what I seeing it looks like it's measured correctly.. https://www.dropbox.com/s/qx3kx0hv39pvazf/Screenshot%202014-01-16%2011.22.07.png its a screenshot of situation for which I get this values in code: `Width of whole widget and just the textview: 453 330` `Computed data for Test appointment: 0.53636366 lines, max num of chars 29.830507` you can clearly see it take more than 1/2 of the line:/ – simekadam Jan 16 '14 at 10:24
  • what is the code you are using also pass the font size i feel that it's because of this – vipul mittal Jan 16 '14 at 10:29
  • TextView.getTextSize() – simekadam Jan 16 '14 at 11:37