-1

In Android, I want to make particular text bold in a paragraph. I stored that paragraph in the string resource of my project.Now I need to use that string as an argument to Canvas, drawtext() method. Where it will draw the specified string and make the text bold where i specified it as Bold using tag <b>.

I tried adding bold tag <b> and retrieve it using Html.fromHtml(_),but not worked,Used gettext() and getString().

Still i can't find the answer. I want it as a string,but not to put in a text view.I have tried spanned text or character sequence Please get me a solution.

Varun Chandran
  • 647
  • 9
  • 23
  • 2
    Possible duplicate of [how to make a specific text on TextView BOLD](https://stackoverflow.com/questions/14371092/how-to-make-a-specific-text-on-textview-bold) – ADM Dec 27 '17 at 06:00
  • 1
    Since you are doing it on canvas using `drawText`, u cannot use tag. But u can try with `Paint` object. – Sivakumar S Dec 27 '17 at 06:31
  • @ADM I specifically mention i am not working with textview, its simple to do that in textview. Please dont say everything as duplicate? – Varun Chandran Jan 31 '18 at 04:21
  • Thats why there is a word you see which says **Possible**. – ADM Jan 31 '18 at 04:58

5 Answers5

3
Actually the issue was, in the String resource if we write `<b>` its will be converted to string itself.
And when Html.fromHtml() tries to convert they found no tags,
So,

For < we have to use  &lt;
For > we have to use  &gt;
For & we have to use  &amp;

Retrieve the text as spanned text and pass it to drawtext(). It worked !! Anyway thanks for the efforts.

Varun Chandran
  • 647
  • 9
  • 23
2

You can do this by setting pint style to BOLD for specific characters and also the start and end index of the string i.e. to be bold should be known you can do this as-

Suppose the string is

<string name="bold_text">This is the Bold text</string>

Following is the to be done in view class

//create two paints one is regular and another is bold
private Paint mPaintText;
private Paint mPaintTextBold;

private String textToDraw;

// initialize them
mPaintText = new Paint();
mPaintText.setColor(Color.WHITE);
mPaintText.setStyle(Style.FILL);
mPaintText.setTextSize(32f);

mPaintTextBold= new Paint();
mPaintTextBold.setColor(Color.WHITE);
mPaintTextBold.setStyle(Style.FILL);
mPaintTextBold.setTextSize(32f);
mPaintTextBold.setTypeface(Typeface.DEFAULT_BOLD);

textToDraw = getString(R.string.bold_text);

// Now in on draw method of view draw the following text if you are drawing 
// text on canvas it means you already have start point let it be be 
// startX and startY, index of the bold string be boldStart and boldEnd in 
// our case it will be 12 and 16

String normalStartString = mTextToDraw.substring(0, boldStart);
String normalEndString = mTextToDraw.substring(boldEnd);
String boldString = mTextToDraw.substring(boldStart, boldEnd);

Paint.FontMetrics fm = mPaintText.getFontMetrics();
float height = -1 * (fm.ascent + fm.descent);

// drawing start string
canvas.drawText(normalStartString, startX, startY - height, mPaintText);

// drawing bold string
float width = mPaintText.measureText(normalStartString) + startX;
canvas.drawText(boldString, width, startY - height, mPaintTextBold);

// drawing end string
width += mPaintTextBold.measureText(boldString);
canvas.drawText(normalEndString, width, startY - height, mPaintText);
Satender Kumar
  • 627
  • 4
  • 7
-1

You can use WebView, this supports more tags than Html.fromHtml().

yourWebView.loadData("<b>Hello bold Text</b>", "text/html", "utf-8");

https://developer.android.com/reference/android/webkit/WebView.html

Tenten Ponce
  • 2,436
  • 1
  • 13
  • 40
-1

This can be implemented in two ways. In strings.xml

<string name="bold_text">This text is <b>Bold</b></string>

and in your activity xml file

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/bold_text" />

In Java code

String htmlAsString = getString(R.string.bold_text);
textView.setText(Html.fromHtml(htmlAsString));
-1

In your values folder inside the strings.xml you can use tag like:

<string name="string_hello"><b>Hello</b></string>

And use in your Activity file like.

your_textView_or_anything.setText(R.string.string_hello);
Vishal Sharma
  • 51
  • 2
  • 10