Good afternoon. Could you help me with a clickable span? The fact is that when I assign a clickable substring at the end of an origin String, not only the text becomes clickable, but also the entire TextView area to the right of the substring, which is bad for me (it is necessary that only a substring is clickable. Can you advise me something ? match parent for my textview is required. Unfortunately, programmatically adding " " to the original string is also unacceptable to me. Pastebin link with code: here. I created a video on my Dropbox:
bad work my programm:
dropbox
I want it to work like this
dropbox
My TextView needs the following layout
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text_view);
SpannableString spanable = new SpannableString("my clickable span");
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "click span", Toast.LENGTH_SHORT).show();
}
};
spanable.setSpan(clickableSpan, 13, 17, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spanable);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
set all text using length.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text_view);
String strService = "my clickable span";
int serviceStart = strService.indexOf("my clickable span");
int serviceEnd = serviceStart + "my clickable span".length();
SpannableString spanable = new SpannableString(strService);
ClickableSpan clickableSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "click span", Toast.LENGTH_SHORT).show();
}
};
spanable.setSpan(clickableSpan, serviceStart, serviceEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(spanable);
textView.setClickable(true); //add clickable
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
Related
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;
I want make a layout like this .
Initially we arranged buttons and text in succession.
However, if there is a line break, it will not be displayed as desired.
Do you know the library or the means to realize this?
My English is still not so good, though, Sorry.
String str="text1 button1 text2 button2 etc.";
SpannableString spanString=new SpannableString(str);
//You can use SpannableStringBuilder also
ClickableSpan buttonSpan1 = new ClickableSpan() {
#Override
public void onClick(View view) {
// Do something
}
};
ClickableSpan buttonSpan2 = new ClickableSpan() {
#Override
public void onClick(View view) {
// Do something
}
};
spanString.setSpan(
buttonSpan1,
str.indexOf("button1"),
str.indexOf("button1") + String.valueOf("button1").length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanString.setSpan(
buttonSpan2,
str.indexOf("button2"),
str.indexOf("button2") + String.valueOf("button2").length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Consider the sentence
"Mike has commented on Davids review at Pizza Planet"
This sentence may wrap onto 2 lines, but I want the words "Mike", "Davids" and "Pizza Planet clickable items".
If I use separate TextViews for each of these word, and also for the non clickable words in between, How can I do this so that they wrap nicely onto 2 lines?
You can use spannable strings for your needs, check this out:
TextView textView = (TextView) findViewById(R.id.textView);
SpannableString user = new SpannableString("Mike");
ClickableSpan userClickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
//click on user
}
};
user.setSpan(userClickableSpan, 0, user.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableString commentedOnUser = new SpannableString("Davids");
ClickableSpan commentedOnUserClickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
//click on commentedOnUser
}
};
commentedOnUser.setSpan(commentedOnUserClickableSpan, 0, commentedOnUser.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
SpannableString place = new SpannableString("Pizza Planet");
ClickableSpan placeClickableSpan = new ClickableSpan() {
#Override
public void onClick(View textView) {
//click on place
}
};
place.setSpan(placeClickableSpan, 0, place.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
textView.setText(user);
textView.append(" is has commented on ");
textView.append(commentedOnUser);
textView.append(" review at ");
textView.append(place);
Now when it's all part of one TextView, use "\n" to break the line.
I have a string in database that is like:
string f = "this is the first link and this is the second link"
textview1.TextFormatted = Html.FromHtml(f);
url =?
Intent i = new Intent(Android.Content.Intent.ActionView,url);
StartActivity(i);
the number of links in the string is different. I want to make all link in textview clickable and when user click on each of them, the url of that link send to another activity.
When you setText using Html.fromHtml, the '' are replaced as UrlSpans in textView.
You can get each of url spans and set clickable spans for onClick function.
Refer to this for the solution code.
I have achieved the same using SpannableStringBuilder.
Simply initialize the TextView that you want to add 2 or more listeners and then pass that to the following method that I have created:
private void customTextView(TextView view) {
SpannableStringBuilder spanTxt = new SpannableStringBuilder(
getString(R.string.read_and_accept));
spanTxt.setSpan(new ForegroundColorSpan(ContextCompat.getColor(activity, R.color.black_30)), 0,
getString(R.string.read_and_accept).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanTxt.append(" ");
spanTxt.append(getString(R.string.t_and_c));
spanTxt.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
Utils.redirectUserToUrl(activity,"http://luxit-driver-terms.tookan.in");
}
}, spanTxt.length() - getString(R.string.t_and_c).length(), spanTxt.length(), 0);
spanTxt.append(" and ");
spanTxt.setSpan(new ForegroundColorSpan(ContextCompat.getColor(activity, R.color.accent)), 48, spanTxt.length(), 0);
spanTxt.append(getString(R.string.privacy_policy));
spanTxt.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
Utils.redirectUserToUrl(activity,"http://luxit-driver-privacypolicy.tookan.in/");
}
}, spanTxt.length() - getString(R.string.privacy_policy).length(), spanTxt.length(), 0);
view.setMovementMethod(LinkMovementMethod.getInstance());
view.setText(spanTxt, TextView.BufferType.SPANNABLE);
}
In Xml,use android:textColorLink to add custom color for ur link text
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:textColorLink="#000000" />
I am using a text view in my aap, which is having plane text as well as a hyperlink. Now when I click on hyperlink then link open with default browser. But in actual I dont want to open default browser. Actually I want to register OnClickListener on hyperlink and want to perform other.
I searched on internet and I got this solution...
Control onclicklistener in autolink enabled textview
But this is not helpful for me.
Anyone can tell me that how I can perform this.
Thanks in advance
you can use a Spannable object
final Spannable span = new SpannableString(text);
span.setSpan(new ClickableSpan() {
#Override
public void onClick(View v) {
}
}, 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
where text is your hyperlink
Remove android:autoLink="web" if this property setted into XML.
TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
when you want to open in browser use this code
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));
if you want to perform some operation register onclick listener for textview and perform.
textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Try doing this add
in your
main.xml
<TextView
android:id="#+id/yourTVID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="performSomeAction" />
in your SomeActivity.java
public void performSomeAction( View v){
//Perform your action
}
Try this, it should solve your problem. This method will return a Spannable String which have part of it clickable.
Before calling the below method you should Create CharSequence from the String then convert it to Spannable
CharSequence charSequencce = testView.getText();
Spannable spannable = (Spannable) charSequencce;
public SpannableStringBuilder addClickToPartsOfString(Spannable charSequence, String[] stringsToAddClick, final OnHyperLinkClickListener onClickListener) {
SpannableStringBuilder ssb = new SpannableStringBuilder(charSequence);
for(final String s : stringsToAddClick) {
int index1 = charSequence.toString().indexOf(s);
int index2 = (s.length() + index1);
ssb.setSpan(new ClickableSpan() {
#Override
public void onClick(View widget) {
onClickListener.onClick(s);
}
}, index1, index2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return ssb;
}
I've just made a library aiming to simplify this. See Textoo. You can achieve the same with code like:
TextView locNotFound = Textoo
.config((TextView) findViewById(R.id.view_location_disabled))
.addLinksHandler(new LinksHandler() {
#Override
public boolean onClick(View view, String url) {
if ("internal://settings/location".equals(url)) {
Intent locSettings = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(locSettings);
return true;
} else {
return false;
}
}
})
.apply();
Internally the library converts existing links in your textview / string resources (android system parse html links in string resources into Span for you already) into custom ClickableSpan and capture clicks into calls to your handlers.
This relieve you from having to calculate and hard coding the position of clickable spans to add. Thus make it easier to externalize your text into string resources and better for localization.