Reputation: 43
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>"));
}
Upvotes: 4
Views: 7394
Reputation: 3561
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.
Upvotes: 1
Reputation: 43798
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>"));
Upvotes: 6