0

I have a CheckBox with a string that says "I have read and understood the terms and conditions". Now I want to make the words "terms and conditions" to a link which opens a alertdialog where the terms and conditions can be read. Nothing special.

I'm thinking something in the line of:

<string name="cont_agree">I have read and understood the <a ref="open alertdialog">terms and conditions.</a></string>

Is it possible, and what should I use where it now says "open alertdialog"? If it can't be done this way, how should I do?

Addition: To open a url you would use this code:

<string name="cont_agree"><a ref="http://www.stackoverflow.com">Stackoverflow</a></string>

But how do you open a alertdialog, or say another screen, from a string? I have seen apps who does this so it is possible, of course, but how?

EDIT: This is the code I use for the SpannableStringBuilder:

SpannableStringBuilder text = new SpannableStringBuilder();
text.append(getString(R.string.before));
//Now create a ClickableSpan
ClickableSpan clickableSpan = new ClickableSpan() {  
            @Override  
            public void onClick(View view) {                

                d.show(); //Here dialog will be displayed
            }  
        };
//Now append the TOS string
text.append(getString(R.string.popup));
//Declare that the "TOS" string is a clickableSpan
text.setSpan(clickableSpan, getString(R.string.before).length(),getString(R.string.before).length()+getString(R.string.popup).length(), 0); //Check on API setSpan method
((CheckBox)findViewById(yourview)).setMovementMethod(LinkMovementMethod.getInstance()); 
((CheckBox)findViewById(yourview)).setText(text, BufferType.SPANNABLE);

I still get some markers at the first "text.append" line. Multiple markers at this line:

  • Return type for the method is missing
  • Syntax error on token ")", { expected after this token
  • R.string.before cannot be resolved to a type
  • Syntax error on token ")", invalid VariableDeclaratorId
  • Syntax error on token "append", Identifier expected after this token
andysando
  • 1,192
  • 2
  • 11
  • 21
  • 2
    Check this link [SO](http://stackoverflow.com/questions/12069811/android-hyperlinks-on-textview-in-custom-alertdialog-not-clickable) – Srikanth Roopa Aug 18 '13 at 12:57
  • Yes I saw that one, but he is trying to open links FROM an alertdialog if I'm not mistaken. I want to open an alertdialog from a regular string used in a checkbox. So I couldn't find an answer there. – andysando Aug 18 '13 at 17:16
  • I really need help with this. Anyone got a clue? – andysando Aug 21 '13 at 07:43
  • i've never tried such implementation @andysando post some code so that others can help.. – Srikanth Roopa Aug 21 '13 at 08:55

1 Answers1

2

First setup your dialog

Dialog d = new Dialog(context);
d.setTitle... etcetc

In your values.xml create 2 string

<string name="before">I have read and understood the</string>
<string name="popup">TOS</string</string>

Now you can use SpannableStringBuilder

SpannableStringBuilder text = new SpannableStringBuilder();
text.append(getString(R.string.before));
//Now create a ClickableSpan
ClickableSpan clickableSpan = new ClickableSpan() {  
            @Override  
            public void onClick(View view) {                

                d.show(); //Here dialog will be displayed
            }  
        };
//Now append the TOS string
text.append(getString(R.string.popup));
//Declare that the "TOS" string is a clickableSpan
text.setSpan(clickableSpan, getString(R.string.before).length(), getString(R.string.before).length()+getString(R.string.popup).length(), 0); //Check on API setSpan method
((CheckBox)findViewById(yourview)).setMovementMethod(LinkMovementMethod.getInstance()); 
((CheckBox)findViewById(yourview)).setText(text, BufferType.SPANNABLE); //AAAAND WE'RE DONE!
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
Tizianoreica
  • 2,142
  • 3
  • 28
  • 43
  • Thank you very much, I'm trying this right now. But I can't get the text.append-line to work. I get multiple markers at that line but append is the first. "Syntax error on token "append", Identifier expected after this token" What does that mean? – andysando Aug 21 '13 at 17:46
  • Sorry there was an errore in my code. it was R.String with capital S, but S must be in lower case, so R.string.before and R.string.popup – Tizianoreica Aug 22 '13 at 08:05
  • Aah, should have thought of that. Will try it later and get back! Thanks! – andysando Aug 22 '13 at 09:50
  • *doh* I had put the code outside the bracket...so it was on the wrong place! Works perfectly, thank you very much! – andysando Aug 24 '13 at 06:30