0

I have added links to text that is surrounded by [square brackets] in a popup dialog box. The links however, are not clickable (nothing happens when they are pressed). I can't work out why(!)

Here is my dialog box activity:

public void popupDefinition(CharSequence term, LinkedHashMap<String, String> dictionaryMap){
    SpannableString definition = new SpannableString(dictionaryMap.get(term)); // grab the definition by checking against the dictionary map hash
    Linkify.addLinks(definition, pattern, scheme); // find text in square brackets, add links

    AlertDialog alertDialog = new AlertDialog.Builder(ListProjectActivity.this).create(); // create a dialog box
    alertDialog.setMessage(definitionFormatted); // set dialog box message
    alertDialog.show(); // actually display the dialog box
    }

'scheme' and 'pattern' are defined earlier, as follows:

final static Pattern pattern = Pattern.compile("\\[[^]]*]"); // defines the fact that links are bound by [square brackets]
final String scheme = "http://example.com/"; // THIS IS NOT WORKING

Why, when I tap the links that appear (they appear nicely underlined in blue), do they not cause any response?

I'm not actually trying to launch URL links (I'll be redirecting the ACTION_VIEW intent when it does occur), but I need to confirm that some sort of response is happening before I get to that...

CaptainProg
  • 5,610
  • 23
  • 71
  • 116

1 Answers1

3

I'm not actually trying to launch URL links

Since you don't need to use URLs, don't bother with trying to create a custom Linkify scheme since it only creates URLSpans. You simply want to start an Activity from a keyword in a TextView. As I stated in one of your similar, but not duplicate, questions I would use a custom span, introducing ActivitySpan:

public class ActivitySpan extends ClickableSpan {
    String keyword;
    public ActivitySpan(String keyword) {
        super();
        this.keyword = keyword;
    }

    @Override
    public void onClick(View v) {
        Context context = v.getContext();
        Intent intent = new Intent(context, AnotherActivity.class);
        intent.putExtra("keyword", keyword);
        context.startActivity(intent);
    }
}

There are no bells or whistles here, this span takes a [keyword] that you surrounded in brackets and passes it to the another Activity.

While I don't like the idea of using Linkify because of URLSpans, its pattern matching and span creation is great, so I copied and modified it:

private void addLinks(TextView textView, Pattern pattern) {
    SpannableString spannable = SpannableString.valueOf(textView.getText());
    Matcher matcher = pattern.matcher(spannable);

    // Create ActivitySpans for each match
    while (matcher.find())
        spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Set new spans in TextView
    textView.setText(spannable);

    // Listen for spannable clicks, if not already
    MovementMethod m = textView.getMovementMethod();
    if ((m == null) || !(m instanceof LinkMovementMethod)) {
        if (textView.getLinksClickable()) {
            textView.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }
}

As a note, this method doesn't remove the [brackets] surrounding each keyword, but you can easily do this in the while-loop.

To use this, simply pass a TextView and Pattern to addLinks() in onCreate() and Voila!


A working example for you:

public class Example extends Activity {
    Pattern pattern = Pattern.compile("\\[[^]]*]");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        popupDefinition("Example: A [pattern] or [model], as of something to be [imitated] or [avoided]");
    }

    // It seems like you can call "popupDefinition(dictionaryMap.get(term));" rather than pass both.  
    public void popupDefinition(String string){
        SpannableString spannable = new SpannableString(string);
        Matcher matcher = pattern.matcher(spannable);

        // Create ActivitySpans for each match
        while (matcher.find())
            spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        // Create a new TextView with these spans and enable the clickable links
        TextView textView = new TextView(this);
        textView.setText(spannable);
        textView.setMovementMethod(LinkMovementMethod.getInstance());

        // Create and display an AlertDialog with this TextView
        AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setView(textView)
                .create(); 
        alertDialog.show(); 
    }


    public class ActivitySpan extends ClickableSpan {
        String keyword;
        public ActivitySpan(String keyword) {
            super();
            this.keyword = keyword;
        }

        @Override
        public void onClick(View v) {
            Context context = v.getContext();
            Toast.makeText(context, keyword, Toast.LENGTH_SHORT).show();
        }
    }
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • My input text is currently a string. How do I convert it to a TextView? – CaptainProg Sep 11 '12 at 19:55
  • I don't really understand the question. Are you having trouble passing the Spannable to your AlertDialog? – Sam Sep 11 '12 at 20:15
  • I mean - my input text that goes into addLinks() is a String - I retrieve it from a HashMap. The problem is that I can't put a String into the above solution - I understand I must be missing something fundamental here... As I understand it, TextView is a text editor object? – CaptainProg Sep 11 '12 at 20:17
  • 1
    Simply pass the string to `SpannableString.valueOf()` in the first line of `addLinks()`. – Sam Sep 11 '12 at 20:19
  • Can I remove all references to a TextView? I'm assuming not, as there must be something special about TextView that allows it to contain links (although I was under the impression that that was the purpose of SpannableStrings)... Hurr.. This could take some time to figure out. Will mark your answer as correct as I have no doubts that it is, even if I can't work it out(!) – CaptainProg Sep 11 '12 at 20:30
  • Boom. Got it. I needed to declare a TextView. I am well aware this is super-simple stuff I'm tripping up on. Thanks for your patience Sam :) – CaptainProg Sep 11 '12 at 20:33
  • Actually got it? (You generously mark an answer as correct sometimes even though it hasn't helped you too much.) If you are trying to display these links in your AlertDialog remember that you can use `setView()` to pass the TextView with your new links in the `AlertDialog.Builder`. I can easily give you an example if you want. – Sam Sep 11 '12 at 20:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16565/discussion-between-captainprog-and-sam) – CaptainProg Sep 11 '12 at 20:48
  • Seriously, thanks very much for this. I'll add a bounty to this when I can and send you the extra rep. You deserve it! This has been a syntactical nightmare for me, and now I can see the correct way to lay my code out. Cheers – CaptainProg Sep 11 '12 at 22:05
  • 2
    To clarify for anyone else who comes across this problem, the reason I couldn't click my links was because I was using setMessage() rather than setView(). Setting as a message removes the ability for text links to do anything other than look pretty. – CaptainProg Sep 11 '12 at 22:16