Reputation: 1100
I have a TextView
in which I want to show the last portion of a String
. Normally if I set a large text in TextView
the first portion of text will be shown in TextView
. But my requirement is to show the last portion (Last full stop will be shown to left of TextView
). I don't know the text size, but it will be large. I have to use single line and I don't want to use ellipses (3 dot).
Upvotes: 0
Views: 200
Reputation: 2211
Check out this,
TextView tvTextView;
String text = "YOUR LONG TEXT WILL BE HERE", subString = "",
int textViewWidth = 100;
int numChars;
tvTextView.setText(text);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
textViewWidth = displayMetrics.widthPixels; // you need to subtract paddings and margins if needed
Log.d("textview_width", "width " + textViewWidth + " length " + text.length());
Paint paint = tvTextView.getPaint();
for (numChars = 1; numChars <= text.length(); ++numChars) {
if (paint.measureText(text, 0, numChars) >= textViewWidth) {
break;
}
}
subString = text.substring(text.length() + 1 - numChars, text.length());
int dots = subString.split("\\.").length * 2;
if(text.length() <= numChars - 1){
subString = text;
}
else {
subString = text.substring(text.length() + 1 + dots - numChars, text.length());
}
tvTextView.setText(subString);
Upvotes: 2