Skip to content Skip to sidebar Skip to footer

Android Multiple Clickable Strings In Textview

I am creating a small Android app. I would like to display a text in a textview with multiple parts to click on. (Each should show some different message) Finally I managed to find

Solution 1:

as i understand you want to make multiple part of textview clickable.

this code worked for me!

SpannableStringss=newSpannableString("this is a text");
ss.setSpan(newmyClickableSpan(1),0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(newmyClickableSpan(2),5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(newmyClickableSpan(3),8, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

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

just make custom ClickableSpan to handle the click event

publicclassmyClickableSpanextendsClickableSpan{

    int pos;
    publicmyClickableSpan(int position){
        this.pos=position;
    }

    @OverridepublicvoidonClick(View widget) {
        Toast.makeText(getApplicationContext(), "Position "  + pos + " clicked!", Toast.LENGTH_LONG).show();
    }

}

Post a Comment for "Android Multiple Clickable Strings In Textview"