How to unhyperlink links programmatically? - android

I have a large body of text that includes web urls and emails. I created a SpannableString object and used Linkify class to set up hyperlinks for the web urls and emails. However, perceived phone numbers also get linked. So even though I have instances of 5-digit numbers such as "12345", this numeric sequence also are being linked. When clicked, a prompt comes up asking to call the number. Linkify thnks it is a phone number. How can I go back through and un-link specific text that I know should not be linked?
Here is how I have linked everything
final SpannableString s = new SpannableString(context.getText(R.string.message));
Linkify.addLinks(s, Linkify.WEB_URLS);
textView.setText(s);
textView.setMovementMethod(LinkMovementMethod.getInstance());
Thank you in advance.

Related

ACTION_CALL intent, with Linkify?

I have a couple Activities with TextViews that contain phone numbers. What I need is that when the user clicks on one, the phone dials the number. And by "dial", I mean dial, not bring up the dialer with the number preloaded, waiting for the user to click "Dial".
In other words, I want Intent.ACTION_CALL, not Intent.ACTION_DIAL.
Now keep in mind, I'm entirely new to Android development, and I'm just beginning to figure out how to put things together.
My first attempt was simple. In my XML:
<TextView
android:id="#+id/textPhoneNumber"
android:text="#string/the_phone_number"
android:autoLink="phone"
android:clickable="true"
android:onClick="clickPhone"
/>
Then in my java:
public void clickPhone(View view)
{
String phoneNumber = getString(R.string.the_phone_number);
Uri uri = Uri.parse("tel:" + phoneNumber);
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(uri);
startActivity(callIntent);
}
Having read more about how autolink is supposed to work, that's clearly more work than should be necessary, but it did work...
Until I had to do the same with a phone number that included letters. You know the kind: 1-800-BUY-MY-STUFF.
The problem, Linkify.PHONE_NUMBERS doesn't recognize strings containing letters as phone numbers, so my string would not be rendered as a link. The onClick would still file, though, and I'd still get the behavior I wanted.
So, I went digging into what autoLink actually did, and read up a bit on Linkify, and removed my onCLick and tried to use a custom pattern:
Pattern matchEverything = Pattern.compile("^.*$");
TextView textPhoneNumber = (TextView)findViewById(R.id.textPhoneNumber);
Linkify.addLinks(textPhoneNumbermatchEverything, "tel:");
Since my TextView contained only a phone number, I figured I'd keep the Regex simple. And this worked fine.
Except that I think I am getting Intent.ACTION_DIAL instead of Intent.ACTION_CALL.
What's the simplest way to get phone number to display in an Activity as a clickable link, and to have clicking that link dial the phone, instead of just bringing up the dialer?
You can just skip the whole linkify business by simulating the same behavior:
Use SpannableString to underline the text in TextView:
String udata="1-800-BUY-MY-STUFF";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);
then in XML, set the TextView's text color to #05c5cf
After that it's a matter of calling your clickPhone method.

Html content in android not displaying in textview android

I am working on android applications. In my app I am getting the html content and setting it to text view. My html data is displaying in the textview but at one point it stopped. i .e only half of the paragraph is displaying in the textview.
The data is
"The Internet is a global system of interconnected computer networks that use the standard Internet protocol suite (TCP/IP) to serve several billion users worldwide. It is a network of networks that consists of millions of private, public, academic, business, and government networks, of local to global scope, that are linked by a broad array of electronic, wireless, and optical networking technologies. The (water A1c <5.0) may be detrimental in certain populations, such as the elderly and those with cardiovascular disease."
In the above paragraph upto "The (water A1c" the data is displaying in the textview and from there the data is not displaying. Th remaning data is cutted. I tried to trim the data but it didnt work. Please give me any suggestions for this.
Mycode:
Textview.setText((Html.fromHtml(data)));
The below content is being cut in the textview. It is not displaying.
<5.0) may be detrimental in certain populations, such as the elderly and those with cardiovascular disease."
String htmlStr = "<b>" +
context.getResources().getString(R.string.yourText)+
"</b>";
textView.setText(Html.fromHtml(htmlStr));
TextView myTextview;
myTextview= (TextView) findViewById(R.id.my_text_view);
htmltext = <your html (markup) character>;
Spanned sp = Html.fromHtml( htmltext );
myTextview.setText(sp);
Try something like below.
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("Hello Everyone"));
Actually it is the problem with the < symbol.
Just use the ASCII value of < symbol. and that is <.
Take a look here for more ASCII values.
Hope this will help. :)
As Aby Mathew suggested, you can display your data in TextView if you replace < by its HTML equivalent
Code that can be used:
data=data.replace("<","<");
Textview.setText((Html.fromHtml(data)));
Hope it helps :)

Android Linkify Special Phone Numbers

Hy! My TextView display string that consists of simple text, adresses, phone numbers, e-mail adresses and web urls. I am using Linkify and it works fine for e-mail adresses and web urls, but problem is with phone numbers because Linkify link me numbers from adresses, IDs and I want that it only link only phone numbers that start like this: "+385" and rest can be "021 348 600" for example. So I made regex but I dont know how to implment it into Linkify. Also I think my regex is OK. Here is my code:
MyTextView=(TextView)findViewById(R.id.tvContact);
Spanned sp= Html.fromHtml( getString(R.string.huge_string_contact));
MyTextView.setText(sp);
Pattern pattern = Pattern.compile("+385[0-9]");
Linkify.addLinks(MyTextView , Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);
That Regex wont do a thing. Try this.
Linkify.addLinks(MyTextView, Pattern.compile("385\\s\\d+\\s\\d+"),"");
Edit: Last parameter should be an Url scheme string. But we don't need that so it can be "".

putting hyperlinks in TextViews - but not knowing the address in advance

I need to localize an Android app in many languages. But the text also contains a local web link like www.theLink.com, www.LinkForOtherLanguage1.it, www.yetAnotherLinkForOtherLanguage2.fr,... you get the idea :)
I know this way to linkify...
Pattern pattern = Pattern.compile("www.theLink.com");
Linkify.addLinks(textView, pattern, "http://");
But here I need to know the addresses and put them in the code. Is there any way without changing the code for each language?
Many thanks
You could put the links in a string array and get them by position according to what language it is

how to use span to call in android?

i have a textview whose text i am setting as a big string.
this string has several parts like telphone no, mail , browsing address.
i have found how to use the browsing address in a span to open up the link in a browser?
here is the code
TextView tv = (TextView) findViewById(R.id.infotest);
SpannableString ss = new SpannableString(getResources().getString(R.string.clientaddress));
ss.setSpan(new URLSpan("http://"+getResources().getString(R.string.clientaddress)), 0, 23,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
how do i do the same for telephone text.
i mean how do i span the telephone text to call a the number when the user presses that specific part of the textview string.
You have to set it from the xml file.
For example:
android:id="#+id/mobile_number"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:autoLink="phone"
Available values for the autoLink field is: web, email, phone, map, all
Can you use Linkify?
Linkify.addLinks(tv,Linkify.PHONE_NUMBERS);
ETA or try using tel:// instead of http?

Categories

Resources