Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting textSize programmatically

Tags:

android

textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.result_font)); 

The following code works, but the R.dimen.result_font is taken as a much bigger value than it really is. Its maybe about 18sp-22sp or 24sp according to the screen size ... But the size set here is at least about 50sp. Can someone please recommend something ?

like image 979
Rakeeb Rajbhandari Avatar asked Dec 04 '13 01:12

Rakeeb Rajbhandari


People also ask

How do I set font size in programmatically?

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).

How to set TextView size dynamically in android?

You need to use another function setTextSize(unit, size) with unit SP like this, tv. setTextSize(TypedValue. COMPLEX_UNIT_SP, 18f);

How to change Text size from java in android?

Use setTextSize() to set values in dp.

How to fix size of TextView in android?

I made fixed size column and scrollable textview. Firstly I remove size tag from shape drawable resource. And I add the following code for each column. LayoutParams params = new TableRow.


1 Answers

You have to change it to TypedValue.COMPLEX_UNIT_PX because getDimension(id) returns a dimen value from resources and implicitly converted to px.

Java:

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,                       getResources().getDimension(R.dimen.result_font)); 

Kotlin:

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,                       resources.getDimension(R.dimen.result_font)) 
like image 142
Glenn Avatar answered Sep 22 '22 00:09

Glenn