4

I have a textview and I want to display 0.0 in normal fontSize of the TextView and then the StringResult() in a smaller text size.

If I add "0.0" + it's not working. Why?

public String StringResult(){
String displayLbl = this.getResources().getString(R.string.displayLbl);
TextView myUnitLbl = (TextView)findViewById(R.id.lbl_unit_res);
String myFinalLbl = displayLbl + " " + myUnitLbl.getText() + " ";
return myFinalLbl;
}


public void cmd_rst(View v){
TextView lblText = (TextView) findViewById(R.id.lblresult);
lblText.setText("0.0" + Html.fromHtml("<small>" + StringResult() + "</small>"));
}
1
  • lblText.setText(Html.fromHtml("<small>" + StringResult() + "</small>")); //only this works Commented Nov 21, 2013 at 20:38

2 Answers 2

6

Thats because the Spanned returned from Html.fromHtml gets used as a normal String (thereby loosing any formatting) when you try to concatenate it with "0.0". To make this work, pass the whole stuff into fromHtml:

Html.fromHtml("0.0<small>" + StringResult() + "</small>")

The same principle applies in more complex cases:

lblResult.setText(Html.fromHtml(String.format("%.1f", myCalc) + " " + "<br>" +
    "<small>" + StringResult() + "</small>"));
Sign up to request clarification or add additional context in comments.

2 Comments

YES. It was simple, thanks. But how about in this case more complex? lblResult.setText(String.format("%.1f", myCalc) + " " + "\n" + Html.fromHtml("<small>" + StringResult() + "</small>"));
Thanks Henry! It was pretty obvious that I had to include everything under Html.fromHtml() as you suggested. Works like a charm!!!
1

just

Html.fromHtml("0.0<small>" + StringResult() + "</small>")

changed to

Html.fromHtml("0.0<small>" + StringResult() + "</small>").toString()

Because fromHtml() method takes argument of Spanned type not String type.

1 Comment

Hmm, I don't get that. Html.fromHtml() returns a Spanned, if you call toString() on it the formatting will be lost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.