Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How size units in Android are applied?

When adding a view in an axml file, it is possible to simply specify the size and the units of the view's attribute, for example:

<TextView
    android:TextSize = "10sp"
    android:layout_marginTop = "10dp" />

But when applying a size programmatically, what are the units applied for the size sp? dp? px?

TextView tv = new TextView();
tv.TextSize = 10;

Also, how can I convert units?

like image 446
prodev1 Avatar asked Jul 07 '26 12:07

prodev1


1 Answers

sp is the default used for TextView.TextSize:

Set the default text size to the given value, interpreted as "scaled pixel" units. This size is adjusted based on the current density and user font size preference.`

Use the SetTextSize method if you need a different unit type (ComplexUnitType):

textview.SetTextSize(ComplexUnitType.Px, 20);
like image 75
SushiHangover Avatar answered Jul 14 '26 20:07

SushiHangover



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!