Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android set autoLink attribute programmatically

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

like image 741
mathkid91 Avatar asked Oct 21 '15 11:10

mathkid91


1 Answers

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

like image 184
Acca Emme Avatar answered Oct 26 '22 02:10

Acca Emme