2

how can we show some additional information about a link in android i have seen examples that open links in the browser but that is not the case what i want

 TextView tv = (TextView) findViewById(R.id.textView1);
String text = "This is just a test. Click this link here <a href=\"http://www.google.com\">Google</a> to visit google.";
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(Html.fromHtml(text));

I have tried some code like above but it is for opening in a browser where else i want something like the below image

enter image description here

nikhil_969
  • 45
  • 4
  • 1
    I don't understand what you want to do? Presumably if you click the links in the screenshot, they will open in a browser, too? – LairdPleng Mar 26 '14 at 12:57
  • 1
    used two TextView first one contain your info-text and second contain link – M D Mar 26 '14 at 12:58
  • 1
    how can we get the info about a link – nikhil_969 Mar 26 '14 at 13:02
  • 1
    What you need to do is to parse the YouTube link for the video ID and then get the thumbnail image for it. See this question: http://stackoverflow.com/questions/8841159/how-to-make-youtube-video-thumbnails-in-android – anthonycr Mar 26 '14 at 13:33

4 Answers4

1

That can be done using a URLSpan.

TextView tv = (TextView) findViewById(R.id.textView1);
SpannableString text = new SpannableString("This is just a test. Click this link here to visit google.");
text.setSpan(new URLSpan("http://www.google.com", 37, 41, 0)); 
tv.setText(text);

There are also several types of other Spans that can be really useful to style text. The 37 and 41 are the start and end indexes of the text to style.

Also here is an excellent blog post by Flavien Laurent on the many uses of Spans.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • 1
    I wanted to display extra info. ex: if the person posts a youtube link then show some kind of image related to the link or som info.PLz help – nikhil_969 Mar 26 '14 at 13:20
0

I think splitting it into 2 text views is the best option. Here is a similar thread:

android: html in textview with link clickable

Hope this helps.

Community
  • 1
  • 1
LordSquall
  • 161
  • 7
0

You can use basic HTML to format your text in TextView. You can do something like bold, italic, link, etc. Here is a sample:

String code = "<p><b>First, </b><br/>" +
        "Please press the link below.</p>" +
        "<p><b>Second,</b><br/>" +
        "Please insert the details of the event."
        "<a href='http://www.google.com'>click here</a></p>";

TextView tv = (TextView) findViewById(R.id.textView1);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(Html.fromHtml(code, this, null));
tv.setTextSize(16);
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
0

What you are looking for is the OG tags from the link

This can be done by using JSoup and getting the information about the link and then extracting the OG tags from that.

You can checkout a simple library I made for the same task.

https://github.com/Priyansh-Kedia/OpenGraphParser

Priyansh Kedia
  • 171
  • 1
  • 6