4

ScreenShot

I have a "Contact Us" Activity in my android application. On clicking the contact number, I want to copy the contact number to the dialler so that the user can dial that number. Similarly, on clicking the email, I want the user to go to his mail with that email address copied to the "sendto" field. Is it possible statically by putting that contact number and email in the strings.xml resource file? I have tried this:-

<string name="call_us">Call us at <a href="+91 1234567890">+91 1234567890</a></string>
<string name="email_us">Email us at <a href="abc@gmail.com">abc@gmail.com</a></string>

But, it is not working.

<LinearLayout
    style="@style/StyleHorizontalLinearLayout">

    <ImageView
        style="@style/StyleSmallIcon"
        android:src="@drawable/icon_call"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:textSize="16dp"
        android:text="@string/call_us"/>

</LinearLayout>
rekire
  • 47,260
  • 30
  • 167
  • 264
Rohit Singla
  • 112
  • 2
  • 9
  • How did you make this ui, could you add the related code to your question? In general I would use html to make clickable links in such an activity. – rekire Nov 14 '15 at 15:22
  • @rekire I have added the code. Everything, I want to do statically through layouts and resources. Is it possible? – Rohit Singla Nov 14 '15 at 15:25

1 Answers1

13

I use for such cases this code:

TextView tv = (TextView)findViewById(R.id.text);
tv.setText(Html.fromHtml(getString(R.string.call_us)));
tv.setMovementMethod(LinkMovementMethod.getInstance());

The last line does the clicking logic for you. So when you click that link the magic happens.

You also need to add a schema to the urls:

<string name="call_us">Call us at <a href="tel:+911234567890">+91 1234567890</a></string>
<string name="email_us">Email us at <a href="mailto:abc@gmail.com">abc@gmail.com</a></string>

With that tel: and mailto: your links will work.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • textViewEmailUs = (TextView)findViewById(R.id.text_view_email_us); textViewEmailUs.setText(Html.fromHtml(getString(R.string.email_us))); textViewEmailUs.setMovementMethod(LinkMovementMethod.getInstance()); abc@gmail.com Not Working : ( Even the hyperlink went away. – Rohit Singla Nov 14 '15 at 15:41
  • Thanks a lot, It's not only working. It's running.....;) Once again, thanks a lot. – Rohit Singla Nov 14 '15 at 15:51
  • 1
    Adding the url in strings.xml as shown here + adding `android:autoLink="email"` in the textview worked for me – Riccardo Apr 23 '19 at 09:19