4

I have a TextView in which I have set some text:

lblText.setText("Hello How Are You");

Now I want it in the following way:

Whenever I click on Hello, a Toast should be displayed saying "Hello". Whenever I click on How, a Toast should be displayed saying "How".

And so on.

How to check on which word I clicked and do different things for different words in a TextView

Pramod Ravikant
  • 1,039
  • 2
  • 11
  • 28
  • check this http://stackoverflow.com/questions/9982241/android-clickable-textview-how-to-make-multiple-click-regions-on-text-and-catch and http://stackoverflow.com/questions/9584136/how-to-click-or-tap-on-a-textview-text-on-different-words – Jitender Dev Mar 11 '14 at 12:20

4 Answers4

3

you have to use a SpannableString if you do not want to have more than 1 TextView.

Edit:

For every String you can register a ClickableSpannable, for instance:

private class MyClickableSpannable extends ClickableSpan {

    private final String mStringToShow;


    public MyClickableSpannable(String stringToShow) {
        mStringToShow = stringToShow;
    } 

    @Override
    public void onClick(View widget) {
         Toast.makeText(context, mStringToShow, Toast.LENGHT_SHORT).show();
    }
}

and to set the listener:

String myString = "Hello How Are You"; 
String hello = "Hello";
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(myString);
spannableStringBuilder.setSpan(
   new MyClickableSpannable(hello), startIndexOfHello,
   startIndexOfHello + hello.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • How will I set listener for it. Can you direct me to an example? – Pramod Ravikant Mar 11 '14 at 12:19
  • 1
    @Blackbelt You are too fast. :-) while answering this kinda questions. :-) – Rethinavel Mar 11 '14 at 12:28
  • @Blackbelt i think OP wants a genaral solution of getting a word at particular x,y point of a TextView – pskink Mar 11 '14 at 12:39
  • @pskink `String.indexOf()` is what he needs – Blackbelt Mar 11 '14 at 13:29
  • @blackbelt i think he meant that he has MotionEvent and based on getX/Y he wants to find out the word in a TextView corresponding to that point – pskink Mar 11 '14 at 14:35
  • @pskink how do you know it? I read the question again but what he wants is to have a different onCliclListener for each word (imo) – Blackbelt Mar 11 '14 at 14:36
  • @blackbelt its my inner voice ;) there are too many guys using Toasts instead of Log.d and i think this is the case – pskink Mar 11 '14 at 14:41
  • @pskink `How to check on which word I clicked and do different things for different words`, imo from this statement he wants a different onClickListener for every word – Blackbelt Mar 11 '14 at 14:44
1

This is working example .Understand it and apply it in your code as per requirement.

  SpannableString ss = new SpannableString("Hello How Are You");
    ClickableSpan clickableSpan1 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
           //toast hello
        }
    };
    ClickableSpan clickableSpan2 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
          // toast how
        }
    };
    ClickableSpan clickableSpan3 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
           //toast are
        }
    };
    ClickableSpan clickableSpan4 = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
           //toast you
        }
    };
    tView.setText(ss);
    ss.setSpan(clickableSpan1, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpan2, 5, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpan3, 9, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpan4, 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
0

Try this out. It worked for me very well.

  SpannableString SpanString = new SpannableString(
            "Hello How Are You");

    ClickableSpan hello = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

        Toast.makeText(this, "Hello is clicked", Toast.LENGTH_SHORT).show();
        }
    };

    ClickableSpan how = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

          Toast.makeText(this, "how is clicked", Toast.LENGTH_SHORT).show();

        }
    };

    SpanString.setSpan(hello, 0, 6, 0);
    SpanString.setSpan(how, 6, 9, 0);


    lblText.setMovementMethod(LinkMovementMethod.getInstance());
    lblText.setText(SpanString, BufferType.SPANNABLE);
Rethinavel
  • 3,912
  • 7
  • 28
  • 49
0

Try this:

TextView tv = (TextView)findViewById(R.id.textView);      
    Spannable span = Spannable.Factory.getInstance().newSpannable("Hello How Are You");   
span.setSpan(new ClickableSpan() {  
    @Override
    public void onClick(View v) {  
        Log.d("main", "Helo clicked");
        Toast.makeText(MainActivity.this, "hello clicked", Toast.LENGTH_SHORT).show(); 
    } }, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new ClickableSpan() {  
   @Override
   public void onClick(View v) {  
       Log.d("main", "how clicked");
       Toast.makeText(MainActivity.this, "how clicked", Toast.LENGTH_SHORT).show(); 
   } }, 6, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(span); 
tv.setMovementMethod(LinkMovementMethod.getInstance());
Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25