Rohit Singla Rohit Singla - 3 days ago 5
Android Question

link to send email from android app in xml string resource

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>

Answer

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.

Comments