Add various listeners for text in TextView

I have a TextView in which I set some text:

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

Now I want it as follows:

Whenever I click "Hello", a symbol Toastwith the words "Hello" should be displayed . Whenever I click "How", it should be displayed Toastwith the words "How."

Etc.

How to check which word I clicked and did different things for different words in TextView

+4
source share
4 answers

you need to use SpannableStringif you do not want to have more than 1 TextView.

Edit:

For each row, you can register ClickableSpannable, for example:

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();
    }
}

:

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);
+3

. .

  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);
+1

Try it. It worked very well for me.

  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);
0
source

Try the following:

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());
0
source

Source: https://habr.com/ru/post/1531155/


All Articles