Gnanam R
Gnanam R

Reputation: 407

How to highlight characters in String for Textview?

String = " This is SAMPLE"

I want to increase the size of the SAMPLE alone in String to highlight, but need to be in a single string, this is to set in TextView.

Thanks in Advance.

Upvotes: 1

Views: 2124

Answers (5)

Trung Nguyen
Trung Nguyen

Reputation: 7532

You have two options:

1, Format your string in HTML and use HTML.from("Your text");

2, Use spannable. A complete guide can find link here

Upvotes: 0

Raghu Nagaraju
Raghu Nagaraju

Reputation: 3288

textView.setText(Html.fromHtml("This is <font color='#707070' size='20'>
            SAMPLE</font>"));

Upvotes: 5

Shubhayu
Shubhayu

Reputation: 13552

I think this is possible if you pass the text as html with your highlighted part enclosed in html <bold> tag.

Upvotes: 0

Shaiful
Shaiful

Reputation: 5673

Use Html.fromHtml() in setText() of TextView. Supported Html Tags can be found here Html Tags Supported by TextView

Upvotes: 0

waqaslam
waqaslam

Reputation: 68177

Try this:

String str = "This is <b>SAMPLE</b>";
yourTextView.setText(Html.fromHtml(str), BufferType.SPANNABLE);

Moreover, you may decorate your string (str) with html tags to alter the looks.

Or to highlight a part of text without using html stuff, read this

Upvotes: 4

Related Questions