raja sid
raja sid

Reputation: 83

Find a particular regex word and linkify to open an activity in android

I am getting the text from API, can be random, but It contains expressions like on FB @xyz hi, @abc how u?? 1. I need to create a regex expression starts with @, ends with space. 2. Linkify it to open the user activity like XYZ or ABC. Please help me, I have tried to create a class like,

import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

public class ClickSpan extends ClickableSpan {

    private OnClickListener mListener;

    public ClickSpan(OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View widget) {
        if (mListener != null) mListener.onClick();
    }

    public interface OnClickListener {
        void onClick();
    }


    /**
     * To clickify a textview
     * @param view
     * @param clickableText
     * @param listener
     */
    public static void clickify(TextView view, final String clickableText, 
            final ClickSpan.OnClickListener listener) {

        CharSequence text = view.getText();
        String string = text.toString();
        ClickSpan span = new ClickSpan(listener);

        int start = string.indexOf(clickableText);
        int end = start + clickableText.length();
        if (start == -1) return;

        if (text instanceof Spannable) {
            ((Spannable)text).setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            SpannableString s = SpannableString.valueOf(text);
            s.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            view.setText(s);
        }

        MovementMethod m = view.getMovementMethod();
        if ((m == null) || !(m instanceof LinkMovementMethod)) {
            view.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }

} 

And call like,

clickify(textView, clickText,new ClickSpan.OnClickListener()
     {
        @Override
        public void onClick() {
           startActivity here..
        }
    });

I need help as to how I can get this to work on my activity creating a loop of regex.

Upvotes: 2

Views: 1685

Answers (1)

StenaviN
StenaviN

Reputation: 3697

public class ClickableURLSpan extends URLSpan {
    public ClickableURLSpan(String url) {
        super(url);
    }

    @Override
    public void onClick(View widget) {
        String clickedText = getURL();

            // START your activity here
    }
}

Now you can linkify your text:

CharSequence input = "@xyz hi, @abc how u??!";
SpannableStringBuilder builder = new SpannableStringBuilder(input);

Pattern pattern = Pattern.compile("@.*?\\s");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    int start = matcher.start();
    int end = matcher.end();

    String text = input.subSequence(start, end).toString();

    ClickableURLSpan url = new ClickableURLSpan(text);
    builder.setSpan(url, start, end, 0);
}

textView.setText(builder);
textView.setMovementMethod(LinkMovementMethod.getInstance());

Upvotes: 6

Related Questions