I;ve been trying to create a custom button in android using this tutorial - http://www.gersic.com/blog.php?id=56
It works well but it doesn't say how to change the font size or weighting. Any ideas?
There was another question on here and the only answer was to use html styling but you can't change a font size in html without using css (or the deprecated font tag). There must be a better way of setting the pixel size of the font used on buttons?
HTML, CSS and JavaScript To change the font size of a button, use the font-size property.
Tap the Gear icon that appears at the top of the Android keyboard. Open Preferences. Tap the Keyboard Height option. You'll see seven different options ranging from "Extra-short" to "Extra-tall." The default is "Normal." Tap the option you prefer.
To set Android Button font/text size, we can set android:textSize attribute for Button in layout XML file. To programmatically set or change Android Button font/text size, we can pass specified size to the method Button. setTextSize(specific_size).
You define these attributes in xml as you would anything else, for example:
<Button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" android:background="@drawable/mybutton_background" android:textSize="10sp" /> <!-- Use SP(Scale Independent Pixel) -->
You can find the allowed attributes in the api.
Or, if you want this to apply to all buttons in your application, create a style. See the Styles and Themes development documentation.
Button butt= new Button(_context); butt.setTextAppearance(_context, R.style.ButtonFontStyle);
and in res/values/style.xml
<resources> <style name="ButtonFontStyle"> <item name="android:textSize">12sp</item> </style> </resources>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With