2

I am new to android and:

I have a TextView that can show a phone number.

I want to have that part of the text to be 'touchable' and attempt to make a phone call or something similar.

I have tried to implement implicit intent for when the user clicks the TextView but I can't make it work.

Is there any solution to this? Or a different approach?

Thanks.

2 Answers2

1

First make your TextView clickable by adding below in your layout.xml

<TextView 
...
...
android:clickable="true">
</TextView>

And in your java code inside OnClickListener of that particular TextView Start phone activty as

TextView tv=(TextView) findViewById(R.id.tv_contact);

 tv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
           startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("tel://"+ tv.getText().toString().trim())));

        }
    });'

And if you want link on specific text on textview then this post may help you

Android textview with clickable phone number

Edit

As @Apoorv suggested,you may also use android:autoLink = "phone" as

In the xml file, add the below lines.

<TextView
      ....
      android:autoLink = "phone"
/>

For more info see android:autoLink - Have a Clickable Phone Number link in a TextView, in XML

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
0

You should use built-in linkify. Read this tutorial, easy to understand: http://android.okhelp.cz/linkify-text-link-url-in-textview-text-android-example/

SuppressWarnings
  • 4,134
  • 4
  • 25
  • 33