Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markwon does not handle links

I use Markwon library and have a problem. It doesn't recognize links. I enter links as [name](link) and It displays only name and doesn't highlight and respond to clicks on this name. How can I fix this bug?

My Gradle entries:

implementation "ru.noties:markwon:2.0.1"
implementation "ru.noties:markwon-image-loader:2.0.1"
implementation "ru.noties:markwon-syntax-highlight:2.0.1"
implementation "ru.noties:markwon-view:2.0.1"

Configuration:

public SpannableConfiguration getAboutTextConfig() {

    ImageSizeResolverFitWidth imgSizeResolver = new ImageSizeResolverFitWidth();

    return SpannableConfiguration.builder(this.context)
            .asyncDrawableLoader(AsyncDrawableLoader.create())
            .imageSizeResolver(imgSizeResolver)
            .build();
}

I have also tried this:

In code:

act_txt.setMovementMethod(LinkMovementMethod.getInstance());

In Layout:

    <TextView
        android:id="@+id/act_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:autoLink="web"
        android:focusable="true"
        android:fontFamily="@font/roboto_regular"
        android:letterSpacing="0.03"
        android:lineSpacingExtra="12sp"
        android:linksClickable="true"
        android:paddingBottom="30dp"
        android:textColor="@color/titleTextColor"
        android:textSize="16sp"
        android:textStyle="normal" />

and override LinkResolver and UrlProc

class LinkResolver implements LinkSpan.Resolver {

    @Override
    public void resolve(View view, @NonNull String link) {
        final Uri uri = Uri.parse(link);
        final Context context = view.getContext();
        final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.w("LinkResolverDef", "Actvity was not found for intent, " + intent.toString());
        }
    }
}


@SuppressWarnings("WeakerAccess")
public class UrlProcessorRelative implements UrlProcessor {

    private final URL base;

    public UrlProcessorRelative(@NonNull String base) {
        this.base = obtain(base);
    }

    @NonNull
    @Override
    public String process(@NonNull String destination) {

        String out = destination;

        if (base != null) {
            try {
                final URL u = new URL(base, destination);
                out = u.toString();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        return out;
    }

    @Nullable
    private URL obtain(String base) {
        try {
            return new URL(base);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
like image 995
AlexDeveloper Avatar asked Jan 22 '26 23:01

AlexDeveloper


1 Answers

Glad you resolved your issue. For anyone else chasing this rabbit, setting a LinkMovementMethod on my TextView worked for me.

myTextView.movementMethod = LinkMovementMethod.getInstance()
like image 69
Tom Avatar answered Jan 24 '26 11:01

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!