views:

23

answers:

1

Is there any way that I can link a keyword in a TextView to a file or directory on the user's SD card? My app produces stack trace files when it crashes and I want the user to able to click a link in my About dialog to view either the latest one or the folder containing all of them. (Something like "If this app crashes, please send [link]the latest stack.trace file[/link] to us at [email protected].")

I know it is possible to use the following code to make a Web link but I tried to modify it to use "file:///sdcard/path/to/file/stack.trace" which causes my app to Force Close when the link is clicked.

<string name="strSample">This is Web link to &lt;a href=&quot;http://www.google.com&amp;quot;&gt;Google&amp;lt;/a&gt;!&lt;/string&gt;

final TextView tvSample = (TextView)findViewById(R.id.tvSample);
tvSample.setMovementMethod(LinkMovementMethod.getInstance());
String strSample = getResources().getString(R.string.strSample);
tvSample.setText(Html.fromHtml(strSample));
A: 

You can extend your method by replacing Html.fromHtml(strSample) part which returns Spannable. Create your own Spannable and add ClickableSpan to it. In the onClick method you can place whatever logic you need. Here's quick examle that you can extend

public Spannable getProfileLink(final Context context) {
    final String name = firstName + " " + lastName;
    Spannable spans = SpannableStringBuilder.valueOf(name);
    ClickableSpan clickSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            // TODO - call profile here
            Toast.makeText(context, "Will call profile", Toast.LENGTH_LONG);
        }

    };
    spans.setSpan(clickSpan, 0, name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spans;
}
DroidIn.net
Awesome thanks!
Blu Dragon