Search code examples
javawebviewcodenameonetextfieldfont-size

CodenameOne - Getting the text size inside TextViews or TextAreas


My CodenameOne app has a BrowserComponent in the layout (it's a WebView).

It has also other components like TextFields.

I need that the text in the BrowserComponent has the same size as the text in the TextFields (default value).

I set the text size inside the BrowserComponent in HTML, using the CSS font-size property.

I tried with this instruction:

int size=textField.getStyle().getFont().getSize();

and then the HTML is created, where there is this string inside a suitable tag definition:

"style=\"font-size:"+String.valueOf(size)+"px;\"";

but the result is 0.

Is it possible to get that number to be correct from any method?


Solution

  • First: You should use getUnselectedStyle() and not getStyle() before getFont().

    The getSize() method is for system fonts as explained in the docs. You got the response of SIZE_MEDIUM which isn't what you want.

    Try getHeight() or getPixelSize() after getFont().