1

I want to make a part of my String into a clickable link. I have tried this:

TextView txt = findViewById(R.id.text);
    String s = "Description <a href=\"url\">link text</a> here";
    Spanned spanned;
    txt.setMovementMethod(LinkMovementMethod.getInstance());
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        spanned = Html.fromHtml(s,Html.FROM_HTML_MODE_LEGACY);
    } else {
        spanned = Html.fromHtml(s);
    }
    txt.setText(spanned);
    txt.setLinksClickable(true);

And some other things but I can't bring it to work. What can I do?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jonas
  • 7,089
  • 15
  • 49
  • 110
  • Have your read this: https://stackoverflow.com/questions/12119800/android-set-link-with-a-href-in-textview – Tim Biegeleisen Jun 10 '18 at 14:28
  • I think thats almost the same as my Code. I use `tv1.setMovementMethod(LinkMovementMethod.getInstance());` as well, I tried `setClickable`. It does not work. – Jonas Jun 10 '18 at 14:34

1 Answers1

0

Here is a complete and simple tutorial about the thing that you need:

https://freakycoder.com/android-notes-29-how-to-create-multiple-clickable-links-in-textview-8c6c7ff475cc

Hossein Seifi
  • 1,380
  • 11
  • 29
  • That does not work in my case. I get the a String like `"Description link text here"` from a database. In the tutorial they use a constant String that contains the link. Also they don't open an url, they start and activity... – Jonas Jun 10 '18 at 17:54
  • Is it not possible for you to set your String to a constant String like a TextView and then use that one? – Hossein Seifi Jun 10 '18 at 18:07
  • No that does not make sense in my context. I get different content form a database with different links, so I cannot create a constant for every link... – Jonas Jun 10 '18 at 19:07