linkedin / Spyglass

A library for mentions on Android
Apache License 2.0
387 stars 128 forks source link

Need to redirect user to another screen on click of span #118

Closed raghunandankavi2010 closed 3 years ago

raghunandankavi2010 commented 3 years ago

Is there a callback when user clicks a particular span so that we can redirect user to particular screen?

nhibner commented 3 years ago

Set a custom MentionSpanFactory on the MentionsEditText. The factory allows you to completely control how the spans are created, so you can implement logic to instantiate your own type of clickable mention span to redirect your users as needed.

raghunandankavi2010 commented 3 years ago

@nhibner thank you. Can you put some sample code here or point me to some code to refer to.

nhibner commented 3 years ago

I already referred to code in my previous response -- set a custom MentionSpanFactory on the MentionsEditText (also can set it on the RichEditorView if you're using that).

In code, here's a very rough example of what this could look like:

    // somewhere in your code
    editorView.setMentionSpanFactory(new MyMentionSpanFactory());

    // factory to create our custom mention spans
    public class MyMentionSpanFactory extends MentionsEditText.MentionSpanFactory {

        @NonNull
        @Override
        public MentionSpan createMentionSpan(@NonNull Mentionable mention, @Nullable MentionSpanConfig config) {
            return new NavigationMentionSpan(mention);
        }
    }

    // custom mention span to navigate to a specific activity when clicked
    public class NavigationMentionSpan extends MentionSpan {

        public NavigationMentionSpan(@NonNull Mentionable mention) {
            super(mention);
        }

        @Override
        public void onClick(@NonNull View widget) {
            super.onClick(widget);

            // Navigate to some other screen
            Intent navToActivityIntent = new Intent();
            widget.getContext().startActivity(navToActivityIntent);
        }
    }
raghunandankavi2010 commented 3 years ago

Thank you.

On Tue, Apr 13, 2021, 12:07 AM Nathan Hibner @.***> wrote:

I already referred to code in my previous response -- set a custom MentionSpanFactory on the MentionsEditText (also can set it on the RichEditorView if you're using that).

In code, here's a very rough example of what this could look like:

// somewhere in your code
editorView.setMentionSpanFactory(new MyMentionSpanFactory());

// factory to create our custom mention spans
public class MyMentionSpanFactory extends MentionsEditText.MentionSpanFactory {

    @NonNull
    @Override
    public MentionSpan createMentionSpan(@NonNull Mentionable mention, @Nullable MentionSpanConfig config) {
        return new NavigationMentionSpan(mention);
    }
}

// custom mention span to navigate to a specific activity when clicked
public class NavigationMentionSpan extends MentionSpan {

    public NavigationMentionSpan(@NonNull Mentionable mention) {
        super(mention);
    }

    @Override
    public void onClick(@NonNull View widget) {
        super.onClick(widget);

        // Navigate to some other screen
        Intent navToActivityIntent = new Intent();
        widget.getContext().startActivity(navToActivityIntent);
    }
}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/linkedin/Spyglass/issues/118#issuecomment-818038319, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABAFPKOE2YOTHP7A65H3V3DTIM4U5ANCNFSM42QEDDRQ .