Android Linkify Special Phone Numbers - android

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 "".

Related

Extract phone number from TextView text

I have a TextView with following words:
Google LLC is an 123 American multinational technology 456 company that specializes in Internet-related services and products. These include online advertising technologies, search, cloud computing, software, and hardware. Google was founded in 1998 by Larry Page and Sergey Brin while they were Ph.D. students at Stanford University. Please call (123) 4444-3343
I want to make the number (123) 4444-3343 to a clickable one and taps it will have the user go to the calling mode.
I have tried following code:
textView.setAutoLinkMask(Linkify.PHONE_NUMBERS);
textView.setMovementMethod(LinkMovementMethod.getInstance());
It doesn't work at all. I only want the (123) 4444-3343 to be clickable. How can I achieve this? Thanks.
first option u have is to add in your XML android:autoLink="phone" to your textView
the second option u have is to use Html.fromHtml
txt.setText(Html.fromHtml("..." +
"Please call (123) 456-7890"));
txt.setMovementMethod(LinkMovementMethod.getInstance());
txt.setAutoLinkMask(Linkify.PHONE_NUMBERS);
another way is to use ClickableSpan more info u can find here
How to set the part of the text view is clickable and here

How to unhyperlink links programmatically?

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.

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 :)

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