0

Is it possible to have OnClickListener for specific letter in textView? For example, in my textView I have "today is sunny day". So if I click for example on letter o, I want some action. I know I can do that by putting each letter on separate textview, but in my database I have longer strings.

  • possible duplicate: http://stackoverflow.com/questions/2302867/android-how-to-determine-character-index-of-a-touch-events-position-in-textvie. – Protostome Dec 11 '14 at 11:26
  • refer this link : http://www.programcreek.com/java-api-examples/index.php?api=android.text.style.ClickableSpan – MFP Dec 11 '14 at 11:39

1 Answers1

1

This could work for your purpose.

SpannableString span = new SpannableString("your string");
ClickableSpan clickSpan = new ClickableSpan() {
    @Override
    public void onClick(View yourTextView) {
        //what you want to do on click of it
    }
};
span.setSpan(clickSpan, 5(random int start), 8(random int end), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView yourTextView= (TextView) findViewById(R.id.idOfYourTextView);
yourTextView.setText(span);
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
Giorgio Antonioli
  • 15,771
  • 10
  • 45
  • 70