I have a TexView
which needs to be placed in maxim 2 lines and has a linkable text in it.
If I set LinkMovementMethod to the text view I get a scrollable TextView
and the ellipsize is ignored.
Xml code:
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dip"
android:clickable="true"
android:maxLines="2"
android:ellipsize="end"
android:scrollHorizontally="false"
android:scrollbars="horizontal"
android:isScrollContainer="false"/>
This is my activity:
public class MyActivity extends Activity {
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.text_view);
SpannableStringBuilder captionSpan = new SpannableStringBuilder();
captionSpan.append("a very long text here a very long text here a very long text here a very long text here a very long text here a very long text here a very long text here a very long text here");
captionSpan.setSpan(new CustomClickableSpan(), 1, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(captionSpan);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
public class CustomClickableSpan extends android.text.style.ClickableSpan {
@Override
public void updateDrawState(TextPaint tp) {
tp.setColor(tp.linkColor);
tp.setUnderlineText(false);
}
@Override
public void onClick(View widget) {
}
}
If I do not set the movement method everything is working fine.
This is the shortest solution: final CharSequence text = tv. getText(); final SpannableString spannableString = new SpannableString( text ); spannableString. setSpan(new URLSpan(""), 0, spannableString.
@Davide If it exceeds the TextView's max lines which is set in the XML. To access in Java, it's TextView. getMaxLines(). So for example, if the text would need 10 lines but the max lines is 5 then lines 6-10 will be truncated and I would like to detect when this happens.
Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute. You can either set the text as attribute value directly, or reference a text defined in the strings.
Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.
I found solution!
Instead setMovementMethod() use OnTouchListener.
String text = textView.getText().toString();
SpannableString spanText = new SpannableString(text);
//here set your spans to spanText
textView.setOnTouchListener(new TouchTextView(spanText));
textView.setText(spanText);
I got this onTouchEvent() from LinkMovementMethod class.
static class TouchTextView implements View.OnTouchListener {
Spannable spannable;
public TouchTextView (Spannable spannable){
this.spannable = spannable;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(!(v instanceof TextView)){
return false;
}
TextView textView = (TextView) v;
if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= textView.getTotalPaddingLeft();
y -= textView.getTotalPaddingTop();
x += textView.getScrollX();
y += textView.getScrollY();
Layout layout = textView.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] link = spannable.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(textView);
} else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(spannable,
spannable.getSpanStart(link[0]),
spannable.getSpanEnd(link[0]));
}
return true;
} else {
Selection.removeSelection(spannable);
}
}
return false;
}
}
And sure TextView should have this attributes:
android:maxLines="2"
android:ellipsize="end"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With