4

I'm trying to setup a TextView that contains two lines of text, the second line being shorter than the first one.

Is there a way to set up TextView's Layout instance (acquired via TextView.getLayout()) to truncate the second line, or is there another way that I'm not aware of?

EDIT 1

There were misinterpretations of my intentions: I'm trying to set a textview layouted in a way, that i can set arbitrary text and depending on the length the following happens: If the text is long enough to start a second line, make the first line as wide as the textview's content width is, but the second line should be shorter, for example half as wide. But there should be a second line, if text is long enough.

I checked out abstract class Layout and it's subclasses, there is a function

public int getLineEnd(int line)

that returns the position of the last character for a line. A way could be to set the text, then check out how wide the second line is layouted and then change the text to a shorter version. Anyways, that seems to be a hacky way, which could be made much cleaner and more stable, if I could set a Layout instance myself (via a setter method, such as getter

TextView.getLayout().

Has anyone set a custom Layout to a subclass of Textview?

EDIT 2 / My Own Solution

Found a solution: Now I'm checking if the second line overlaps with the region that was the reason for making the second line shorter. If so, I'm truncating the text. Functions I used:

TextView.getLineCount()
TextView.getLayout().getLineStart(int line)
TextView.getLayout().getLineEnd(int line)
TextView.getPaint().measureText()

Maybe that helps a future anyone.

The question how to set an own layout still remains unanswered, though.

Bondax
  • 3,143
  • 28
  • 35
  • have you tried [setEllipsize](http://developer.android.com/reference/android/widget/TextView.html#setEllipsize%28android.text.TextUtils.TruncateAt%29) – vikki Aug 21 '12 at 15:22

4 Answers4

0

Try using android:maxLines="1" in your XML or textView.setMaxLines(1); in your Activity.

  • Also refer the following link, http://stackoverflow.com/questions/2461824/how-to-programmatically-set-maxlenght-in-android-textview?rq=1 –  Aug 21 '12 at 15:34
0

http://developer.android.com/reference/android/widget/TextView.html#attr_android:maxLines

Makes the TextView be at most this many lines tall.

Must be an integer value, such as "100".

This corresponds to the global attribute resource symbol maxLines.

and you can also use:

http://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize

If set, causes words that are longer than the view is wide to be ellipsized instead of broken in the middle. You will often also want to set scrollHorizontally or singleLine as well so that the text as a whole is also constrained to a single line instead of still allowed to be broken onto multiple lines.

Must be one of the following constant values.

none start middle end marquee This corresponds to the global attribute resource symbol ellipsize.

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
0

You can use the "\n" to mark the different lines in a String. And use the TextView.setGravity() method to define the TextView's Layout. For example,

mTextView.setText("Hello World Hello World\nSecond.");
mTextView.setGravity(Gravity.CENTER);

And the preview of this TextView will be:

Hello World Hello World
        Second
TaoZang
  • 1,690
  • 2
  • 15
  • 15
0

I met the same problem,here is my solution:


here is the code,the truncateText method to calculate the text length and truncate.

public class LastLineControlTextView extends TextView{

private int mMaxLines;
private float mLastLineRatio = 0.5f;
private float mLastLineWidth;
private float mAllowLength;

public LastLineControlTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mLastLineWidth = getWidth()*mLastLineRatio;
    mAllowLength = (mMaxLines-1)*getWidth()+mLastLineWidth;
    if(!TextUtils.isEmpty(getText())){
        truncateText(getText());
    }
}

@Override
public void setText(CharSequence text, BufferType type) {
    super.setText(text, type);
    if(mAllowLength==0)
        return;
    truncateText(text);
}

@Override
public void setMaxLines(int maxlines) {
    super.setMaxLines(maxlines);
    mMaxLines = maxlines;
}

private void truncateText(CharSequence text){
    float width = Layout.getDesiredWidth(text, getPaint());
    int end = text.length();
    while(width > mAllowLength){
        width = Layout.getDesiredWidth(text, 0, end, getPaint());
        end--;
    }
    if(end==text.length())
        return;
    CharSequence processed = text.subSequence(0, end);
    System.out.println("end:"+end+"processed:"+processed);
    setText(processed);
}
}

Azu
  • 51
  • 2