How do I open a browser on clicking a text link in TextView

stodgy.nerd picture stodgy.nerd · Mar 26, 2017 · Viewed 16.6k times · Source

I made an activity that has some text. I made a link clickable in TextView but and it is working fine (link is visible with underline).

But when I click the link it says Unfortunately app has stopped responding Here is my code.

The TextView code:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/home"
    android:text="@string/google" />

The Java code (in protected void onCreate(Bundle savedInstanceState) ):

 TextView txt= (TextView) findViewById(R.id.home); //txt is object of TextView
    txt.setMovementMethod(LinkMovementMethod.getInstance());

The string.xml code:

<string name="google">
    <a href="www.google.com">Home page</a>
</string>

This is what my app shows,

App screenshot

Now if I click Home Page link the error message saying Unfortunately app has stopped not responding appears What should I do?

Please help!

Answer

Adnan Amjad picture Adnan Amjad · Mar 26, 2017

Just add following line in your textview xml.

android:autoLink="web"