How set TextView text both clickable and selectable? - android

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>"));

Related

Adding a clickable web link within Android TextView

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.

Set clickable link with placeholder text in textview within a viewpager

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"

Linkify textView and make text selectable

I have a textView and I want to make the text clickable (urls and phone numbers)
I have successfully been using Linkify
ex
Linkify.addLinks(tvDescription, Linkify.ALL);
Now I would like to make the text selectable using
tvDescription.setTextIsSelectable(true);
It does also work as long as I dont Linkify my textView.
If I use both lines of code
tvDescription.setTextIsSelectable(true);
Linkify.addLinks(tvDescription, Linkify.ALL);
the text will by linkifyed but the text wont be selectable.
I have been trying
tvDescription.setAutoLinkMask(Linkify.ALL);
but this does not seem to work at all.
How can I linkify my textView as well as make my text selectable?
Thanks for any response

Android - Hyperlink missing underline

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)

How to allow the user to select a text range in a TextView (Similar to EditText)

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.

Categories

Resources