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)
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 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>
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 have a textview with fixed height. I want to set a long text, but text comes at the last line getting cut by half, I want to avoid it and want to show a continuation symbol. I am giving an image here, I wan to achieve it like in the image.
Use the ellipsize attribute of TextView. By setting :
<TextView
....
android:ellipsize="end">
</TextView>
if the text is longer than your TextView you will have dots(...) at the end of the TextView.
Two line of string only possible to do like this. set ellipsize = end in text view properties.
you should use \n and special symbols from google code.
I've got a TextView that I would like to allow the user to select a range of text from within it. The TextView takes up the entire width and height of the device (minus some padding and a title at the top). In an EditText if you long-click you get a selection overlay that allows you to set your selection left and right bounds. I'd like this functionality in a TextView. I've read that in API level 9 (2.3) (http://developer.android.com/sdk/android-2.3.html) there are new text selection controls, but I'm having difficulty implementing this. I'm doing this right now:
eic = new InputConnection( bookTextView );
eic.beginBatchEdit();
But it doesn't do anything noticable. Does anyone know how to use InputConnection correctly? Thanks.
Edit: I don't necessarily need to use what I was attempting above. I ultimately want to use either a TextView or an EditText which looks and feels like a TextView and be able to select text using a dragging cursor. Then I would like to manipulate the selected text with various context menu options (or a menu that pops up above the selected text).
Here is an idea.. Add an EditText with a TextView background, Here is an example
<EditText
android:text=" This is not an editable EditText"
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:textColor = "#android:color/white"
android:editable = "false"
android:cursorVisible="false"
android:layout_height="wrap_content"
android:background = "#android:drawable/dark_header">
</EditText>
add this to your xml in the place of TextView
You can enable the TextView's Spannable storage. See Highlight Text in TextView or WebView for an example.
See also:
http://developer.android.com/reference/android/text/Spanned.html
You could display the text in a WebView and enable text selection. If you want to only use a textview/edittext, here is an answer that might help you and here is information on the Spannable class that might help you accomplish what you want.
Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false. My idea is the same as sandy's.
My code is here, hope it may help you:
https://stackoverflow.com/a/11026292/966405
After long internet surfing to find a solution, i prefered create my own class
https://github.com/orionsource/SelectableTextViewer
Goal features:
Easy to use - only one class
Support for text and Html.fromHtml
Can be in ScrollView with correct touches
Cursors can be redefined
Color of selection can be redefined
All the above solutions either too long or not working for me.
What you need is to add just textView.setTextIsSelectable(true)
in your activity or fragment or adapter.