16
<TextView
    android:text="123456789"
    android:autoLink="phone">
</TextView>

I want to create this TextView from code, however I am encountering countless problems.

In the first place, I got halfway creating a TextView and adding this:

tw_phone.setAutoLinkMask(0x04);

This resulted in a clickable TextView, but when you clicked, a toast said "No application can perform this action", or something similar. I also tried with

Linkify.addLinks(tw_phone, Linkify.PHONE_NUMBERS); //and .ALL

but it gave me the same result.

When I decided to ask on StackOverflow, I tried to strip my code down incase there were something wrong with the way I have used Layouts (you never know), but now I'm not even able to make a TextView clickable. This is the code that, in my opinion, should work as it is just a stripped down version of what I am using deeper in my code.

TableLayout table = (TableLayout) findViewById(R.id.tableResult);
TableRow row = new TableRow(this);
TextView tw = new TextView(this);
tw.setText("123456789");
tw.setAutoLinkMask(Linkify.ALL);
row.addView(tw);
table.addView(row);

Can someone write a simple, small example of how you create a TextView, give it a number as text and then allows the user to click on it and choose whatever app they want to open the number with?? If you can point out whats wrong in my code aswell, that would be great, but I would much rather just get the answer straight away. The things I have tried are taken from other StackOverflow questions and answers.

mathkid91
  • 659
  • 2
  • 10
  • 27
  • Maybe the problem is with the text you are setting on the textview, have you tried using "tel:123456789"? – Nanoc Oct 21 '15 at 11:59
  • Tried both places.. In my "working" links, tel: is placed infront of the numbers but only the numbers are clickable. In my dead, unclickable example, nothing is clickable at all.. – mathkid91 Oct 21 '15 at 12:08
  • I was able to do it using tw.setText("1-XXX-XXX-XXXX"); tw.setAutoLinkMask(0x04); and make calls on a click. – Ahmed Faisal Jan 12 '16 at 21:03

2 Answers2

17
TextView tv_contatti2 = new TextView(this); tv_contatti2.setText(contatti);
Linkify.addLinks(tv_contatti2, Linkify.PHONE_NUMBERS);
tv_contatti2.setLinksClickable(true);

where "contatti" has value +39012345678 with international prefix

Acca Emme
  • 356
  • 2
  • 11
2

In order to make text view clickable with any url try following code :

Linkify.addLinks(textView, Linkify.WEB_URLS)

Mudit Goel
  • 196
  • 1
  • 5