I am creating a textview and adding to the layout dynamically. I am using textView.setTextSize(18)
method to set the text size.I tested it on samsung tablet and found that the font size is too small for this screen then I changed the textsize to 25 but it is too big for an emulator(480*800). My problem is to set text size dynamically so that it fits for all the screens.

- 4,003
- 3
- 26
- 43

- 32,082
- 5
- 39
- 53
-
8but this question title is perfect compared to duplicate – vnshetty Jun 21 '12 at 10:54
7 Answers
EDIT: And As I Search on StackOverflow now I found This Question is Duplicate of : This and This
You need to use another function setTextSize(unit, size)
with unit SP
like this,
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f);
Please read more for TypedValue constants.

- 114,585
- 152
- 739
- 1,270

- 34,073
- 11
- 87
- 98
-
I think your answer is not good. Better use value from dimens.xml – Yura Shinkarev Jan 28 '16 at 10:43
You should use the resource folders such as
values-ldpi
values-mdpi
values-hdpi
And write the text size in 'dimensions.xml' file for each range.
And in the java code you can set the text size with
textView.setTextSize(getResources().getDimension(R.dimen.textsize));
Sample dimensions.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">15sp</dimen>
</resources>

- 31,439
- 14
- 68
- 79
-
-
7Probably no need for the DPI qualifier if the dimension is specified in sp as they are scaled by the screen density (and user preferred font size). – Justin Mclean Jan 19 '13 at 02:07
-
Isn't DP better than SP? Will the above work for this issue: http://stackoverflow.com/questions/18497760/having-an-issue-with-textsize-within-textview – Si8 Aug 28 '13 at 21:04
-
-
10
-
39Actually, you need textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.textsize)); – radu122 Mar 26 '16 at 23:20
There is probably no need to use the ldpi
, mdpi
or hdpi
qualifiers in this case.
When you define a dimension in a resource file you include the measurement unit. If you use sp units they are scaled according to the screen density so text at 15sp should appear roughly the same size on screens of differing density.
(The real screen density of the device isn't going to exactly match as Android generalises screen density into 120, 160, 240, 320, 480 and 640 dpi
groups.)
When calling getResources().getDimension(R.dimen.textsize)
it will return the size in pixels. If using sp it will scaled by the screen density,
Calling setText(float)
sets the size in sp units
. This is where the issue is,
i.e you have pixels measurements on one hand and sp unit
on the other to fix do this:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.textsize));
Note you can also use
getResources().getDimensionPixelSize(R.dimen.textSize);
instead of getDimension()
and it will round and convert to an non fractional value.

- 1,615
- 13
- 14
After long time stuck this issue, finally solved like this
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.textsize));
create folder like this res/values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textsize">8sp</dimen>
</resources>

- 8,621
- 10
- 63
- 98
-
6Why TypedValue.COMPLEX_UNIT_PX and now TypedValue.COMPLEX_UNIT_SP? – portfoliobuilder Jul 06 '15 at 01:07
-
-
@SudarshanTaparia create folder here res/values/dimens.xml and add your text size. – NagarjunaReddy Nov 19 '19 at 13:21
In Style.xml pre-define the style:
<style name="largeText">
<item name="android:textAppearance">@android:style/TextAppearance.Large.Inverse</item>
<item name="android:textStyle">bold</item>
</style>
in code:
text.setTextAppearance(context, R.style.largeText);

- 3,144
- 4
- 32
- 53
I think You should use the textView.setTextSize(float size) method to set the size of text. textView.setText(arg) used to set the text in the Text View.

- 361
- 1
- 4
- 12
-
Android Text size will be always specifying in sp, not in dp or float values – Jijo Thomas Jun 13 '16 at 06:38
float currentSize = textEdit.getTextSize(); // default size
float newSize = currentSize * 2.0F; // new size is twice bigger than default one
textEdit.setTextSize(newSize);

- 97
- 1
- 4
-
I don't see any consideration for the screen size in this answer. – Lay González Sep 10 '14 at 15:49
-
In my opinion the question is about font size. So I don't understand why you complain about absence of references to sceen size. Default font has appropriate size in most of cases. So it is better to think about font size in terms of bigger/smaller default size, i.e. independetly of screen size. So I wrote a solution which can be used to set font size relative to default font size. – trunikov Sep 19 '14 at 12:39