Change text size programmatically android

400+ Android & Flutter Code

Example code for android + flutter app developers.

Saturday, January 3, 2015

How to change TextView font size in android

TextView widget display text on android application. we can set or change TextView font size statically by declarative syntax in xml layout file or programmatically at run time in java file. even we can use an xml file source to define font size.

the following example code demonstrate us how can we define TextView font size in xml layout file and how can we uses dimens.xml to reference font size. in this example we did not changes any coding in java file, so here we only include the layout xml file and dimens.xml file.

Dimension
we defined dimension value in xml layout file as example 50sp, 20pt, 30dp. dimension is specified with a number followed by a unit of measure. android supported the following units of measure, those are dp, sp, pt, px, mm, in.

dp means Density-independent-Pixels. this is an abstract unit that is based on the physical density of the screen.

sp means Scale-independent-Pixels. SP as like dp but it is also scaled by the user’s font size preference. Sp is recommended unit for measure.

pt means Points — 1/72 of an inch based on the physical size of screen. px describe Pixels which corresponds to actual pixels on the screen. mm is Millimeters which is based on the physical size of the screen. in describe inches which also based on the physical size of the screen.

Читайте также:  Пульт для андроид приставка приставки

Источник

romannurik / AndroidManifest.xml

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

manifest . >
.
Make sure your app (or individual activity) uses the
theme with the custom attribute defined. —>
application android : theme = » @style/AppTheme » . >
.
application >
manifest >

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

public class MyActivity extends Activity <
protected void onCreate ( Bundle savedInstanceState ) <
super . onCreate(savedInstanceState);
.
// Pass in the theme attribute that’ll resolve to the
// desired button style resource. The current theme
// must have a value set for this attribute.
Button myButton = new Button ( this , null , R . attr . myButtonStyle);
myButton . setText( » Hello world » );
ViewGroup containerView = ( ViewGroup ) findViewById( R . id . container);
containerView . addView(myButton);
>
>

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

The convention is to put the

And TextView initialization does:

But it doesn’t recognize fontFamily attribute :S Is it a normal behaviour?

This comment has been minimized.

Copy link Quote reply

jrub commented Jun 24, 2015

Very useful indeed, thanks Roman. This should definitely be included into the official documentation for Themes & Styles (http://developer.android.com/guide/topics/ui/themes.html)

This comment has been minimized.

Copy link Quote reply

ursusursus commented Feb 19, 2016

For me this does not apply layout params like layout_width, layout_height when I have them in my style. Only default wrap_content LayoutParams gets created in both dimensions

Источник

Читайте также:  List all views android
Оцените статью