0

I need to implement a code of TextView setOnClickListener.I set the text (How are you). In this text, I need only are will be clickable not normal text. Please suggest me. Thanks.

sunil
  • 300
  • 3
  • 20
  • just look at this http://stackoverflow.com/questions/19227276/how-can-i-make-several-clickable-parts-of-text-in-textview – Farhan Shah Jun 06 '14 at 11:16

3 Answers3

0
String sourceString = "How <b>are</b> you"; 
//mytextview.setText(Html.fromHtml(sourceString));   

 android.text.style.ClickableSpan can solve you problem.

    SpannableString ss = new SpannableString(sourceString);
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            startActivity(new Intent(MyActivity.this, NextActivity.class));
        }
    };
    ss.setSpan(clickableSpan, 4, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView textView = (TextView) findViewById(R.id.hello);
    textView.setText(ss);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
bimal chawla
  • 447
  • 1
  • 7
  • 21
  • Thanks. @bimal chawla below are showing underline. i need only same bold text – sunil Jun 06 '14 at 11:20
  • @sunil check now. i edited my answer to show "are" or clickable text as bold. You can do this by making custom string like sourceString in code. – bimal chawla Jun 06 '14 at 11:26
0

You can make it using ClickableSpan. Suppose your text is "I agree to the Terms Of Use and Privacy Policy of TT" and you want to make Privacy Policy as clickable, then you can make like this.

 Spannable WordToSpan = new SpannableString("I agree to the Terms Of Use and Privacy Policy of TT");        
    WordToSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#32a1fc") ), 31, 46, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    WordToSpan.setSpan(privacyPolicy, 31, 46, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      ((TextView)findViewById(R.id.tv_terms_condition)).setText(WordToSpan);

 ClickableSpan privacyPolicy = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LegalDisclaimer.this,PrivacyPolicy.class));
            }
            @Override
            public void updateDrawState(TextPaint ds) {// override updateDrawState
                ds.setUnderlineText(false); // set to false to remove underline
            }
        };

In onClick() method you can do whatever you want..

Xan
  • 74,770
  • 16
  • 179
  • 206
user
  • 245
  • 1
  • 4
  • 12
0
        SpannableString ss = new SpannableString("Hi this is @Naveen. I'll meet @Peter in the evening..Would you like to join @Sam??");

    ClickableSpan clickableSpanNaveen = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            //Do Stuff for naveen
        }
    };
    ClickableSpan clickableSpanPeter = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            //Do Stuff for peter
        }
    };
    ClickableSpan clickableSpanSam = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            //Do Stuff for sam
        }
    };

    ss.setSpan(clickableSpanNaveen, 11, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpanPeter, 29, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpanSam, 76, 79, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    TextView contentTextView=(TextView)userHeader.findViewById(R.id.contentTextView);
    contentTextView.setText(ss);
    contentTextView.setMovementMethod(LinkMovementMethod.getInstance());
Wilson
  • 176
  • 1
  • 11