andysando
andysando

Reputation: 1192

Android: Open alertdialog from part of string?

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:

Upvotes: 0

Views: 421

Answers (1)

Tizianoreica
Tizianoreica

Reputation: 2236

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!

Upvotes: 2

Related Questions