0

I want to put a link in the text by the radio-button. Is there any way of doing this? The radio button inherits from TextView so there should be.

I have managed to use Html.fromHtml() to get the text into the radio button, but the link is neither underlined nor clickable.

final RadioButton sButton = 
    (RadioButton)findViewById( 
        R.id.radiobutton );
sButton.setText( 
    Html.fromHtml( 
            "1 månad (99:-) <a href='banan.html'>info</a>" ),
    BufferType.SPANNABLE );
sButton.setLinksClickable( true );
sButton.setMovementMethod( LinkMovementMethod.getInstance() );

Info should appear as a link and be clickable.

The Science Boy
  • 351
  • 5
  • 22
  • why not a radio without text and a link positioned beside it? – Einacio Aug 23 '11 at 12:36
  • I don't know. Why not? My way seems more intuitive. This way I don't have to put layouts inside the radiogroup. I guess that would work, but I haven't tried it yet. If no one provides a solution with HTML I will try it. – The Science Boy Aug 23 '11 at 12:43
  • 1
    look here: http://stackoverflow.com/questions/1997328/android-clickable-hyperlinks-in-alertdialog similar issue (-; – tamshi Aug 23 '11 at 12:46
  • in actual plain html, radios doesn't even have associated text, you have to associate a label element, who can have a link inside it – Einacio Aug 23 '11 at 12:52

2 Answers2

1

Turns out my example code above works! The problem was that I had used android:autoLink for the radio button, which somehow negated any code I added. So without the autoLink, the code above works. The setLinksClickable is not necessary.

The Science Boy
  • 351
  • 5
  • 22
0

Put this in the xml where you define your radio button.

android:autoLink="0x01"

That will set the TextView contained within your RadioButton to automatically linkify URLs. For information about different values that can be passed in see here

Once you do that you wont need to use fromHtml() anymore you can simply use setText("http://yoursite.com");

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • But won't that make the whole text into a link? I need to specify both the destination URL and a text that should represent that link. I don't want just an URL as the RadioButton text. – The Science Boy Aug 24 '11 at 06:54
  • Yes, I tried this and the link remains as text but the URL becomes a link in the display. This means that it _should_ be possible to create proper links in some way. – The Science Boy Aug 24 '11 at 08:00
  • Find the code for the Linkify class. I imagine it would be possible to create a custom version of that class that would allow you to make the clickable text say whatever you want. This question: http://stackoverflow.com/questions/4599786/android-linkify-both-web-and-mentions-all-in-the-same-textview has an answer with a Linkify class that I modified to work with twitter #hashtags, and @mentions. – FoamyGuy Aug 24 '11 at 13:20