I want to make a TextView with a link. I made it with combination of html and bit of java:
// used to enable link navigation on TextView
setMovementMethod(LinkMovementMethod.getInstance())
// TextView with link
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:linksClickable="true"
android:text="#string/link"/>
// #string/link
<string name="link">Test link</string>
However there is still one issue, the space before actual link text is underlined like this:
Why is that and how could it be fixed?
// #string/link
<string name="link1">Test link</string>
You can use the white space in xml as string use . XML won't take white space as it is. it will trim the white space before setting it. So use instead of single white space.
Use CDATA in string to use HTML tags and use Html.fromHtml() method to set the text.
Implementation below:
Set the text using Html.fromHtml() in your Activity class.
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(Html.fromHtml(getString(R.string.link)));
textView.setMovementMethod(LinkMovementMethod.getInstance());
In strings.xml modify as below:
<string name="link">Test <![CDATA[link]]></string>
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 am trying to display some text in a TextView with #string, the problem is i cant seem to be able to change the color of part of the text. I dont want to use Java to do this as all other questions suggest, since this is the default TextView text.
I've tried using <string name="myString"><font color="#00ff00">My String</font></string> and font fgcolor="#00ff00" but they dont seem to work.
I've tried with your given string and it is works for me. Here is an example
Create <TextView> inside any layout
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text" />
Create text string inside strings.xml file
<string name="text"><font color="#00ff00">Hello</font> <font
color="#FF0000">World</font></string>
And you can see the result
Your font color is working for me, Note you have missed the closing tag of the string(the backslash to close the string)
<string name="myString"><font color="#00ff00">My String in green</font> and normal</string>
If this doesn't help, Whats the android version you are testing this
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>"));
I use linkify to make a textview work as hyperlink, and it does work nicely. The only issue is the underline is missing, could anyone point me out what could cause the problem? shouldn't the underline come by default?
Thanks!
Take a look at the Spannable params
addLinks(Spannable text,...)
linkify class
SpannableStringBuilder class
-replying to comment-
SpannableStringbuilder implements CharSequence, which can be used in TextView.setText();
So once you finish making your underlined text, you can use TextView.setText() and still use the method your are using.
Or refer to this: How to set underline text on textview?
You can use the xml attribute autoLink="web" for the TextView widget to automatically detect if the content is a web address. Here is an example:
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textColorLink="#color/hyperlink_blue"
android:autoLink="web"
android:textSize="16sp"/>
The attribute textColor is for text other than hyperlinks, which will be 'black' in the above example and the attribute textColorLink is for any text that takes a form of a hyperlink - which will be blue per above.
You can also append other autoLink values by 'piping' them together:
android:autoLink="web|email|map|phone"
This works for TextView, AppCompatTextView (SupportV7/AppCompat), AppCompatTextView (androidx/AppCompat)
I have the following TextView:
<TextView android:id="#+id/theFooBar"
android:autoLink="web"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="#string/fooBar"
android:textColor="#android:color/black"
android:textSize="20sp"/>
And the string:
<string name="fooBar">Foo <u>bar</u>.</string>
This gives my black, underlined text. What if I want blue, non-underlined text for the link (the "bar" part), but I want the rest (the "foo" part) to be black? How could I achieve that?
Add android:textColorLink in TextView to define text color for links
I recommended you to use WebView instead of TextView in this situation:
WebView web = (WebView) findViewById(R.id.theFooBar);
String str = "<font color='blue'>bar</font><font color='black'><u>foo</u></font>";
web.setBackgroundColor(0);
// It will sets the background color from white to transparent.
web.loadData(str, "text/html", "utf8");