multiline in textview not working with sppannable text - android

I am using TextView to display text. I need to display max 3 line of text. To accomplish this , i have used maxLines property.
<TextView
android:id="#+id/textFeedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="3"
android:textColor="#color/colorFeedDate"
android:textSize="#dimen/d13sp"/>
Now i need to set sppannable text in textview. Following is my code to set clickable span.
SpannableString spannableString = new SpannableString(requiredString);
ClickableSpan clickSpan = new ClickableSpan() {
#Override
public void onClick(View view) {
}
#Override
public void updateDrawState(TextPaint ds) {
ds.setColor(ContextCompat.getColor(mContext, R.color.colorAppBlue));
ds.setUnderlineText(false);
}
};
spannableString.setSpan(clickSpan, indexOfStart, indexOfEnd - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.append(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
It display okay. Now when i tap on textview then sometimes multiline property not seems working. Textview text scrolls vertically. And display following view.
Anyone has faced such issue?
Thanks.

Related

Cannot remove clickListener from TextView and set it to spannable text

I have the following situation: there is the textview on my screen. This textview has onClickListener. Than I need to set Spannable text to this textView. The part of the spannable text has it's own click listener. For this purpose, I clear textview listener, but in this situation, TextView is not clickable at all:
public void setExpandableClickListenerForPaymentsMethod(SpannableStringBuilder spannableStringBuilder) {
loanDetailTv.setOnClickListener(null);
ClickableSpan linkSpan = new ClickableSpan() {
#Override
public void onClick(#NonNull View widget) {
getController().onRestructingClick();
}
};
spannableStringBuilder.setSpan(linkSpan, 45, 129, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
loanDetailTv.setText(spannableStringBuilder);
}
What's the reason and how can I solve it?
try to make the textview not clickable like this:
textview.clickable = false;

Spannable string in multiline text not working

In my Android project I have to show a spannable text in a text view. The text view is limited to two lines and I'm suppose to ellipsize it. But when I set the spannable string, it causes some problems if the string length exceeds two lines. When it exceeds two lines '...' which is suppose to appear is not showing. And further more it scrolls to the end of the text on touch text.
This is my text view in the layout
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end" />
This is my code implementation
SpannableStringBuilder spannable = new SpannableStringBuilder(item.getText());
spannable.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
openPost();
}
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
}, getText().length() - getNameLength(),getText().length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
notificationText.setText(spannable, TextView.BufferType.SPANNABLE);
notificationText.setMovementMethod(LinkMovementMethod.getInstance());
How can I fix this

How to use ClickableSpan, but ignore clicks from the TextView

The following code shows how I'm applying a pressed state using a custom ClickableSpan and selector. However, the pressed state is applied whenever I press anywhere on the TextView, not just the ClickableSpan. How do I stop this?
Note: it does not call onClick, but does apply state_pressed from the selector. I want it to do neither.
MyView.java
SpannableString spanned = new SpannableString("click here");
spannable.setSpan(new MyClickableSpan() {
#Override
public void onClick(View widget) {
doSomething();
}
}, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanned);
textView.setMovementMethod(LinkMovementMethod.getInstance());
MyClickableSpan.java
public abstract class MyClickableSpan extends ClickableSpan {
#Override
public abstract void onClick(View view);
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
the TextView
<TextView
android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textColorLink="#color/my_selector" />
my_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/my_color_pressed" />
<item android:color="#color/my_color" />
</selector>
Edit note: Added TextView code
Next example works as you're expecting:
Spannable span = SpannableStringBuilder.valueOf("Hello clickable span!");
span.setSpan(new MyClickableSpan(), 6, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(span);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());
Now span applied:
Here's MyClickableSpan() that's only show Snackbar to indicate that "click" is handled:
class MyClickableSpan extends ClickableSpan {
#Override
public void onClick(View widget) {
Snackbar.make(getWindow().findViewById(android.R.id.content), "Click on span!", Snackbar.LENGTH_LONG).show();
}
}
We've got:
Click/tap outside the "spanned" text will do nothing
Click/tap on "spanned" part of text will show Snackbar
That's it. Please let me know if you need any additional info.
You have to set MovementMethod to the TextView which has the Span.
textView.setMovementMethod(LinkMovementMethod.getInstance());
You can refer below code I have tested it.
String s = "This is custom string click Here";
SpannableString spanned = new SpannableString(s);
spanned.setSpan(new MyClickableSpan() {
#Override
public void onClick(View widget) {
Log.i("main", "Link clicked");
}
}, s.length() - 10, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanned);
textView.setMovementMethod(LinkMovementMethod.getInstance());
TextView
<TextView
android:id="#+id/my_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#drawable/my_selector"
android:textColorLink="#color/colorPrimary" />
You can use tutorial here
In your code you have define
android:textColor="#android:color/white"
instead of this you have to define your selector like below
android:textColor="#drawable/my_selector"
rest of code is as it is you have to changed only one line that android:textColor="#drawable/my_selector" that i explain above.
You can do this way:
TextView textView = (TextView)findViewById(R.id.textView);
Spannable span = Spannable.Factory.getInstance().newSpannable("test link span");
span.setSpan(new ClickableSpan() {
#Override
public void onClick(View v) {
Log.i("main", "Link clicked");
Toast.makeText(HomeScreenActivity.this, "link clicked", Toast.LENGTH_SHORT).show();
} }, 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set the "test " spannable.
span.setSpan(cs, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set the " span" spannable
span.setSpan(cs, 6, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Hope this will help you.

Android: Prevent changing background color of text in TextView with ClickableSpan when touched

I just want to make some text clickable in TextView, so I used ClickableSpan, but the background color of text is changed when I touched it.
This is my code:
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
// do something
}
};
SpannableString ss = new SpannableString("ClickableSpan Test");
ss.setSpan(clickableSpan, 0, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
How can I prevent changing background color on touch?
Add this in you textview
android:textColorHighlight`="#000000"

Android: Make a part of TextView's text NOT-clickable at runtime?

I used Android.text.style.ClickableSpan to make a part (Black) of a string (Blue | Black) clickable:
SpannableString spannableString = new SpannableString("Blue | Black ");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
//...
}
};
ss.setSpan(clickableSpan, 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.secondActivity_textView4);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
So Black part of the string is clickable. What I want is that when the user clicks Black, it should make Black Not-clickable, and Blue (another part of the same string) clickable.
So to make Blue clickable, we can call setSpan() on the same spannableString another time. But how can I make Black not-clickable?
You can call removeSpan() to remove any previously added Spans. In this particular case it's very easy, as we hold a reference to the very Span we want to remove:
ClickableSpan clickableSpan = new ClickableSpan()
{
#Override
public void onClick(View view)
{
((SpannableString)textView.getText()).removeSpan(this);
}
};
Another option could be to iterate over all ClickableSpan instances and remove them all, such as:
SpannableString str = (SpannableString)textView.getText();
for (ClickableSpan span : str.getSpans(0, str.length(), ClickableSpan.class))
str.removeSpan(span);
For some reason that I cannot fathom, the documentation for spans is really poor... they are quite powerful!

Categories

Resources