0

I need to make all numbers in a string become links. The expected action when any of these links is clicked is to append the clicked number to an existing string. I managed to linkify the numbers by using the following code:

Pattern myMatcher = Pattern.compile("[0-9]*"); Linkify.addLinks(myString, myMatcher, null);

How can I access and retrieve the clicked number in this case?

I tried looking in other questions related to Linkify but seems all are describing ways to have an action that opens an activity or open the default app for that link type (email address/web URL/etc.)

Thanks in advance for you help :)

Omar
  • 7,835
  • 14
  • 62
  • 108
  • First of Pattern.compile("[0-9]") will matmatch single digits only like 0, 1, 2 etc, not 10, 22, 999, also what exactly do you want to achieve? – pskink Dec 14 '14 at 10:38
  • Thanks for that valuable input pskink. I will fix that. What I want is the following: given a string that contains multiple numbers (the numbers can be of any number of digits), I want to allow the user to "indicate" some specific numbers by clicking on them. The app will have a wizard that will ask "Click on first number". When the user clicks the number he wants, the number will be appended to a textView. Then the system will "Click on second number" and the same should happen(append to TV). – Omar Dec 14 '14 at 14:22

1 Answers1

0

You can Customize Linkify to append any predefiened string(scheme) into that.

Take a look at the following post Android Developer Blogspot (Search for "Custom Linkify")

For clarity I am describing a portion of that post here:

Linkify will automatically append whatever is matched to a scheme that is supplied to it, so for the sake of argument let's assume we have a ContentProvider that matches the following content URI:

content://com.google.android.wikinotes.db.wikinotes/wikinotes/WikiWord

The WikiWord part will be appended by Linkify when it finds a match, so we just need the part before that as our scheme.

Now that we have these two things, we use Linkify to connect them up:

Pattern wikiWordMatcher = Pattern.compile("\\b[A-Z]+[a-z0-9]+[A-Z][A-Za-z0-9]+\\b");
String wikiViewURL =    "content://com.google.android.wikinotes.db.wikinotes/wikinotes/";
Linkify.addLinks(noteView, wikiWordMatcher, wikiViewURL);

Linkify can be used multiple times on the same view to add more links, so using this after the Default Linkify call means that the existing active links will be maintained and the new WikiWords will be added. You could define more Linkify actions and keep applying them to the same TextView if you wanted to.

Now, if we have a WikiWord in the TextView, let's say MyToDoList, Linkify will turn it into an active link with the content URI:

content://com.google.android.wikinotes.db.wikinotes/wikinotes/MyToDoList

and if you click on it, Android will fire the default intent for that content URI.

For this to all work, you will need a ContentProvider that understands that Content URI, and you will need a default activity capable of doing something with the resulting data. I plan to cover these in future blog entries (and soon). In fact, the whole Wiki Note Pad application is currently undergoing some clean up and review, and will then hopefully be released as a sample application.

Shubhang Malviya
  • 1,525
  • 11
  • 17
  • Thanks Shubhang. I've already seen this blogpost. Still I cannot figure out how to read the string that was appended to the URI. I need the number to be appended to a textview on the screen. – Omar Dec 14 '14 at 09:52