I want to set a text with a clickable link in my TextView.
Expected output: More info can be found here.
The String looks like this:
String text = "More info can be found <a href='http://google.com'>here</a>."
I set it to the TextView like this
textView.setText(Html.fromHtml(text));
textView.setMovementMethod(LinkMovementMethod.getInstance());
This should work as is but since my Textview is in a Fragment within a ViewPager clicking it won't do anything.
Anyone has experienced the same issue?
You can use this in your xml attribute
android:autoLink="web"
Related
I am trying to add a link to a website within the text of my TextView.
I'm using Html.fromHtml() method to set the text, and the link is looking as expected, but when I click it, it's not doing anything. I tried setting movementMethod to LinkMovementMethod.getInstance(), but this just gets rid of my formatting, still not working.
agreementText.text = HtmlCompat.fromHtml("I'm familiar with terms of use of this website.", HtmlCompat.FROM_HTML_MODE_LEGACY)
agreementText.movementMethod = LinkMovementMethod.getInstance()
My TextView:
<TextView
android:id="#+id/agreement_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:linksClickable="true"
android:autoLink="all"/>
I actually found the answer in a comment to this post: Android: textview hyperlink
The comment was: "the most correct answer would be to type the HTML anchor tag directly in strings.xml, make sure the autolink property for the TextView is not set, then use setMovementMethod to set the LinkMovementMethod". So if I don't set autoLink or linksClickable, and I set LinkMovementMethod, it's working properly.
I want to add second textview, just after completion of the first one. Linear layout with orientation horizontal didn't help.
Any other alternatives? Please suggest!
Generally, we use HTML text for this information.
We can use as many text styles as we want for different texts. With this way, you can set properties for each texts differently.
For Example: <HTML><text1><text2></HTML>
In the above, you can define text properties independently. Finally place this text in Textview. So we can achieve this using single TextView itself.
Example:
String str = "<a>This is a <font color='#800000FF'> blue text</font> and this is a <font color='red'> red text</font> </a>";
TextView textView = (TextView)findViewById(R.id.text);
textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));
How to achieve this design in xml, please help. I described all I want in this attachment:
You can achieve this by overlapping the TextViews in a ConstraintLayout and adding spaces to the 2nd TextView that match the number of letters of the first one. Or you can use one TextView and style the first part of the string differently.
As a reference check styling String resources with HTML on the developer's website
for example:
textView.setText(Html.fromHtml("<b>Title</b>. The rest of your description"));
UPDATE after comments:
<string name="first_item"><b>Title.</b> Rest of the description!</string>
then programmatically:
String text = getString(R.string.first_item);
Spanned styledText = Html.fromHtml(text, FROM_HTML_MODE_LEGACY);
So, let's assume that I want to show a text such as : "Hello stranger ", using TextView. How would I do that? I realized that I cannot edit only a part of the text in TextView by changing textStyle attribute.
Use yourTextViewName.setText(Html.fromHtml(<b>Hello</b> <i>stranger</i>);
Reference here: https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String,%20int,%20android.text.Html.ImageGetter,%20android.text.Html.TagHandler)
Don't bother with complexity.
Place a TextView with the uneditable text on the left of the EditText. Change the margins and everything that makes it look like a unified view and you're done.
You can define the string in string.xml like this
<string name="text"><b>Hello</b><i>stranger</i></string>
and call it in your TextView like android:text="#string/text". It will work for you.
My textview load html text that contains links (to website, to e-mail address ...)
tv = (TextView)((Activity)mContext).findViewById(R.id.entry_webview);
tv.setText(Html.fromHtml(myPage));
I settv.setMovementMethod(LinkMovementMethod.getInstance()); to make the linke be clickable.
I set tv.setTextIsSelectable(true); to make the text selectable.
What happens? TextView applies just the last setting, in this order case, text will be ONLY selectable and the link(s) won't be clickable, one setting excludes the other other one.
If I set in TextView in XML
android:autoLink="all"
android:textIsSelectable="true"
links don't work (e-mail yes).
Is there a way to make the text both clickable and selectable?
Thanks.
I had the same issue - this solution works for me:
The XML TextView should not have any link/selectable attributes:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Then, set everything programmatically respecting the following order:
textView.setText(Html.fromHtml(myHtml));
Linkify.addLinks(textView, Linkify.WEB_URLS);
textView.setTextIsSelectable(true); // API-11 and above
textView.setMovementMethod(LinkMovementMethod.getInstance());
have you tried using something like this tv.setText(Html.fromHtml("<a href='url'>link</a>"));