Manoj
Manoj

Reputation: 118

multiple onclicklistener on each word of string in text view android

I want to add multiple click able links in text view! get get response of each clicked text.

As show in the attached images blue text are click able.these links does not have fixed position in string.

Upvotes: 4

Views: 925

Answers (2)

m0rpheu5
m0rpheu5

Reputation: 600

This might help,which explains creating tags like in Rss Feed Apps

https://professorneurus.wordpress.com/2013/10/23/adding-multiple-clicking-regions-to-an-android-textview/

private void makeTagLinks(final String text, final TextView tv) {
        if (text == null || tv == null) {
            return;
        }
        final SpannableString ss = new SpannableString(text);
        final List items = Arrays.asList(text.split("\\s*,\\s"))
        int start = 0, end;
        for (final String item : items) {
            end = start + item.length();
            if (start < end) {
                ss.setSpan(new MyClickableSpan(item), start, end, 0);
            }
            start += item.length() + 2;//comma and space in the original text ;)
        }
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setText(ss, TextView.BufferType.SPANNABLE);
    }

Upvotes: 1

Exigente05
Exigente05

Reputation: 2211

TextView textView= (TextView)view.findViewById(R.id.textViewAboutUs);

SpannableString ss = new SpannableString("Your String");

ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View textView) {
        //Do whatever

    }
};

ss.setSpan(clickableSpan, starting_position, end_position, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //spanned string, for multiple string define multiple ss.setSpan

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

Upvotes: 0

Related Questions