2

My question may sound a bit weird but I'll try to explain it better here.

I ahve a TextView in android, at the inferior part of my activity. I want to have it limited to 2 lines, which is easily reachable by adding the following line in the TextView xml element:

android:maxLines="2"

Okay, now we've got it limited to 2 lines. Then, in my Activity, I make:

termsandconditions = (TextView) findViewById(R.id.textView1);
termsandconditions.setText(terms);

Okay, now I've got a big string with the terms and conditions, but limited to 2 lines due to the xml attribute.

Now my question is, how can I cut it after having it limited to 2 lines, and concatenate a string with "Read more"? I don't need it to be in the same textView or whatever, I only want that it looks like:

Terms: blablablalblalbla blal blablalblalblalblalbla lalblalblalblalblalblalb lalblalblalb lalblalblalblalblalb lalblalblalb lalblalblalblalb lalb bla View More.

Thanks and I hope you can understand my problem.

user2281682
  • 77
  • 1
  • 4

2 Answers2

0

You can use setEllipsize (TextUtils.TruncateAt where) method of TextView or the android:ellipsize XML attribute.

public void setEllipsize (TextUtils.TruncateAt where) Added in API level 1

Causes words in the text that are longer than the view is wide to be ellipsized instead of broken in the middle. You may also want to setSingleLine() or setHorizontallyScrolling(boolean) to constrain the text to a single line. Use null to turn off ellipsizing. If setMaxLines(int) has been used to set two or more lines, END and MARQUEE* are only supported (other ellipsizing types will not do anything).

Related XML Attributes

android:ellipsize
Eran
  • 387,369
  • 54
  • 702
  • 768
0

Might be a bit hacky, but here's my suggestion:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    <TextView
            android:id="your_text_view"
            android:text="long long long text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    <TextView
            android:id="link"
            android:text="View More"
            android:onClick="addMoreLinesAndHideThisTextView"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

</RelativeLayout>

Some additional tweaking may be needed, but I think you got the idea.

Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89