Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.fromHtml not working

Tags:

android

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>"));
}
like image 900
dunrad Avatar asked Sep 06 '25 21:09

dunrad


1 Answers

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>"));
like image 197
Henry Avatar answered Sep 08 '25 10:09

Henry