15

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!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
stodgy.nerd
  • 601
  • 4
  • 9
  • 17

4 Answers4

17

Just add following line in your textview xml.

android:autoLink="web"
Adnan Amjad
  • 2,553
  • 1
  • 20
  • 29
  • 2
    My java code doesn't have onClickListener() you should give that code too. Please give full answer from now on if you answer any question t anyone. Anyways, thanks for the help. – stodgy.nerd Mar 26 '17 at 07:51
15

Thanks to those who helped me by their answers

This is what the complete answer looks like.

In the TextView of activityname.xml write android:autoLink="web"

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

In the java file write create onClickListener()

TextView txt= (TextView) findViewById(R.id.home); //txt is object of TextView
    txt.setMovementMethod(LinkMovementMethod.getInstance());
    txt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW);
            browserIntent.setData(Uri.parse("http://www.google.com"));
            startActivity(browserIntent);
        }
    });

No changes in the string.xml file

Thanks to @AdnanAmjad @sarvesh @Mikejess and to the guy who deleted his comment

stodgy.nerd
  • 601
  • 4
  • 9
  • 17
  • 2
    I noticed you pass in the uri to the intent manually. What if you have a long text view with multiple links? How would one pragmatically find which link was clicked and extract that specific url from string resource file? – Mr.Drew Nov 04 '18 at 15:10
5
text_view.setOnClickListener(new OnClickListener(){

     String url = textView.getText().toString();
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);

});
Mike jess
  • 151
  • 1
  • 10
1

Check this one it is working for me i have check the result both for textview and button you can use it and at the place of text you can replace the link

            <Button
                android:layout_width="fill_parent"
                android:layout_height="20dp"
                android:text=" Click To Send Email "
                android:textColor="#FFF"
                android:clickable="true"
                android:background="#cf1414"
                android:linksClickable="true"
                android:id="@+id/emailbutton"/>
sarvesh
  • 662
  • 6
  • 13
  • My java code doesn't have `onClickListener()` you should give that code too. Please give full answer from now on if you answer any question t anyone. Anyways, thanks for the help. – stodgy.nerd Mar 26 '17 at 07:51