Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set multiple spans on a TextView's text (clickable and bold)

    mSpannableString = new SpannableString("12:00PM");

    mSpannableString.setSpan(clickableSpan, 0, 7, 0);
    mSpannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, 7, 0);

    TextView textView = new TextView(getContext());
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(mSpannableString);

    mCancelFrom.setText(getString(R.string.cancel_from_text) + " " + mSpannableString);

i need to set the text to bold and make it clickable. the above code i have written in a dialog. the string "12:00PM" is getting displayed. but there is not bold effect on it and it is not clickable. can you hel me with this?

like image 563
Harshith Avatar asked Aug 10 '16 13:08

Harshith


People also ask

How do you make a clickable span?

SpannableString string = new SpannableString("Text with clickable text"); string. setSpan(new CustomClickableSpan(), 10, 19, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); Text with ClickableSpan .

How do I make Spannable strings bold?

For example, the StyleSpan can be used like this: SpannableString string = new SpannableString("Bold and italic text"); string. setSpan(new StyleSpan(Typeface. BOLD), 0, 4, Spannable.

What is spanned text?

SpannedString. This is the class for text whose content and markup are immutable. This is the interface for text that has markup objects attached to ranges of it. Not all text classes have mutable markup or text; see Spannable for mutable markup and Editable for mutable text.


1 Answers

Try This ,it may be help to you

SpannableString ss = new SpannableString("12:00PM");
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            startActivity(new Intent(SendSMS.this, SendSMS.class));
        }
        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
    };
    ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(clickableSpan, 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    mCancelFrom.setText(getString(R.string.cancel_from_text) + " " + ss);
like image 54
Abhishek Patel Avatar answered Oct 07 '22 00:10

Abhishek Patel