I am trying to set ellipsize of text view. using the following code. I want to add \"view more\" at the end of truncated string after 3 dots. If this would be possible with
This can be achieved during Runtime , all you need to do is check the length of string and add Underlined View More at the end of string like this.
I have used length '20' as an example , you can change according to your requirement.
final TextView result = (TextView) findViewById(R.id.textview);
String text = "I tend to shy away from restaurant chains, but wherever I go, PF Chang's has solidly good food and, like Starbucks, they're reliable. We were staying in Boston for a week and after a long day and blah blah blah blah...";
if (text.length()>20) {
text=text.substring(0,20)+"...";
result.setText(Html.fromHtml(text+"<font color='red'> <u>View More</u></font>"));
}
Check out my library : https://github.com/AhmMhd/SeeMoreTextView-Android
<com.abdulhakeem.seemoretextview.SeeMoreTextView
android:id="@+id/textview
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
usage:
TextView seemoreTv = (TextView) findViewById(R.id.textview)
seemoreTv.setContent("some really long text here.")
it also works great on recyclerview.
It might be late but its the easiest and tested way to handle this issue in recyclerview .
first check the length of textview and set view more if require
if (inventory.getDescription().length()>90) {
inventoryDescription.setText(Html.fromHtml(inventory.getDescription().substring(0,90)+"..."+"<font color='blue'> <u>View More</u></font>"));
}
else inventoryDescription.setText(inventory.getDescription());
and in textview click listener
inventoryDescription.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inventoryDescription.getText().toString().endsWith("View More")) {
inventoryDescription.setText(inventory.getDescription());
}
else {
if (inventory.getDescription().length()>90) {
inventoryDescription.setText(Html.fromHtml(inventory.getDescription().substring(0,90)+"..."+"<font color='blue'> <u>View More</u></font>"));
}
else inventoryDescription.setText(inventory.getDescription());
}
}
});
This will have ellipsize effect.
set Boolean isCheck= true;
put this in the xml:
<TextView
android:id="@+id/txt_id"
android:maxLines="2"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
and the code:
txt_id= (TextView)findViewById(R.id.txt_id);
txt_id.setText("data");
txt_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isCheck) {
txt_id.setMaxLines(10);
isCheck = false;
} else {
txt_id.setMaxLines(2);
isCheck = true;
}
}
}
Try using this library :)
//Add this dependency into App Gradle
implementation 'com.borjabravo:readmoretextview:2.1.0'
Usage:
<com.borjabravo.readmoretextview.ReadMoreTextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/DemoText"
app:colorClickableText="#3F51B5"/>
Check This Link: https://github.com/bravoborja/ReadMoreTextView
Simpler than the accepted answer:
public static final int MAX_LINES = 3;
String myReallyLongText = "Bacon ipsum dolor amet porchetta venison ham fatback alcatra tri-tip, turducken strip steak sausage rump burgdoggen pork loin. Spare ribs filet mignon salami, strip steak ball tip shank frankfurter corned beef venison. Pig pork belly pork chop andouille. Porchetta pork belly ground round, filet mignon bresaola chuck swine shoulder leberkas jerky boudin. Landjaeger pork chop corned beef, tri-tip brisket rump pastrami flank."
textView.setText(myReallyLongText);
textView.post(new Runnable() {
@Override
public void run() {
// Past the maximum number of lines we want to display.
if (textView.getLineCount() > MAX_LINES) {
int lastCharShown = textView.getLayout().getLineVisibleEnd(MAX_LINES - 1);
textView.setMaxLines(MAX_LINES);
String moreString = context.getString(R.string.more);
String suffix = TWO_SPACES + moreString;
// 3 is a "magic number" but it's just basically the length of the ellipsis we're going to insert
String actionDisplayText = myReallyLongText.substring(0, lastCharShown - suffix.length() - 3) + "..." + suffix;
SpannableString truncatedSpannableString = new SpannableString(actionDisplayText);
int startIndex = actionDisplayText.indexOf(moreString);
truncatedSpannableString.setSpan(new ForegroundColorSpan(context.getColor(android.R.color.blue)), startIndex, startIndex + moreString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(truncatedSpannableString);
}
}
});