noties / Markwon

Android markdown library (no WebView)
https://noties.io/Markwon/
Apache License 2.0
2.76k stars 313 forks source link

Handling Link Event in TextView #35

Closed sandeepyohans closed 6 years ago

sandeepyohans commented 6 years ago

Hi @noties, I am new to this library and I am stuck with handling the links on my TextView. On click I am getting the following message: W/LinkResolverDef: Actvity was not found for intent, Intent { act=android.intent.action.VIEW dat=Romans:1:18 (has extras) } I need to pick the text Romans1:18 from the link and format it for further processing. Can you please guide me to use this library?

noties commented 6 years ago

Hello @sandeepyohans !

Something like this should do:

final SpannableConfiguration configuration = SpannableConfiguration.builder(this)
        .linkResolver(new LinkSpan.Resolver() {
            @Override
            public void resolve(View view, @NonNull String link) {

                // process clicked link here, match you custom scheme, launch Activity,
                // or really whatever.

                // just for example:
                if ("Romans:1:18".equals(link)) {
                    showFragment(TheBookFragment.newInstance("Romans", 1, 18));
                }

                // if for example you still want to redirect _native_ link handling
                // you can check `LinkResolverDef` (or instantiate it here and call manually)
            }
        })
        .build();

Markwon.setMarkdown(textView, configuration, "# My markdown");

Hope this helps! Please keep me updated if it doesn't answer your needs, we will work something out 🙌

sandeepyohans commented 6 years ago

Wow! It is working for me superb! Thanks a ton @noties 🙌

sandeepyohans commented 6 years ago

Hi @noties , I am able to display an AlertDialog when a link from Markwon TextView is clicked! Now I want to add Markdwon support to the AlertDialog and display links in it. I am unable to get the TextView from DialogFragment to do so. Can you please guide to solve this problem?

noties commented 6 years ago

Hey @sandeepyohans !

There are multiple way to do so, the most simple would be:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(getContext())
            .setTitle("My title")
            .setMessage(Markwon.markdown(getContext(), "**M**arkdown here"))
            .setPositiveButton(android.R.string.ok, null)
            .create();
}

You can use, but only if you absolutely sure that your markdown doesn't contain tables or images. If you need to support tables and images you can do this:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // please note that it is only an example, as you should create a correctly
    // styled TextView most likely with a help from LayoutInflater and a layout file
    final TextView textView = new TextView(getContext());
    Markwon.setMarkdown(textView, "**M**arkdown here");

    return new AlertDialog.Builder(getContext())
            .setTitle("My title")
            .setView(textView)
            .setPositiveButton(android.R.string.ok, null)
            .create();
}

I hope this helps!

sandeepyohans commented 6 years ago

Hi @noties, Thank you for your reply. The solution you provided looks great to me! As of now there are no tables and images in my markdown, but I do have links in it to display. I need to show another dialog on link click event. I will give a try to the solution you have provided and will get back to you on this. Thanks so much! 🙌