Home » Android » Android programatic button font size in pixels

Android programatic button font size in pixels

Posted by: admin June 15, 2020 Leave a comment


Is it possible to set programatically a font size (in pixels) for the text in a button, without using xml styles? Using Html.fromHtml(..) with font size=”..” didn’t worked.

How to&Answers:

You can use Button.setTextSize() (since the button class extends a simple TextView):

// Set textsize to 20px here
button.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20); 

Note though that it is not recommended to use pixels as a dimension at all, rather use sp when working with font sizes. Otherwise you will run into display problems on many devices due to the many different screens out there.


I’d try setTextSize() (Button inherits from TextView)