67

The official documentation does not seem to answer this, or I can't figure it out.

Element (nevermind the AlertDialog, it happens on any TextView as well):

TextView tv = (TextView) dialog.findViewById(android.R.id.message);

Inconsistency. Case A:

tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
// or tv.setTextSize(14); does the same

Case B:

tv.setTextSize(getResources().getDimension(R.dimen.text_size_small));
// TypedValue makes no difference either.

where values/dimens.xml has it:

<dimen name="text_size_small">14sp</dimen>

Result: font size is not the same, and appears much bigger when retrieving from resource. I'm probably missing something, so what's my mistake, and the most important: why?

-- UPDATE TO FIRST ANSWER --

The fixed number was just an example, as nobody would hard code fixed font sizes in code. So let me rephrase the question:

Why if I get the resource from code, the text size is bigger than when I get the resource from a XML layout? Besides, the question is still the same: how do I retrieve a 14sp unit in code and keep it consistent with the 14sp unit that is set in the layout XML? I did not accept the answer because it does not tell me how to use sp units from resource in code for text size.

On this layout, the font size is different, even if the dimension is the same:

<TextView
            android:id="@+id/my_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/TextBody" />

styles.xml:

<style name="TextBody">
    <item name="android:textSize">@dimen/text_size_small</item>
    <item name="android:lineSpacingMultiplier">1.1</item>
    <item name="android:textColor">@color/body_text_1</item>
    <item name="android:textIsSelectable">true</item>
    <item name="android:linksClickable">true</item>
</style>

See text_size_small there? Why in this case the font size is smaller than in the code, using the same dimen resource?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
davidcesarino
  • 16,160
  • 16
  • 68
  • 109
  • Thank you for raising this issue. I also get it in my project. – anticafe Jan 20 '14 at 10:19
  • "The fixed number was just an example, as nobody would hard code fixed font sizes in code. So let me rephrase the question" - not true at all, having hard coded Dimen values which are overridden by screen sizes in attrs xml files are a sound way of controlling text size in Android – Rowan Berry Dec 16 '20 at 02:33
  • When I was asking the question I did not know even the basics of font dimension retrieval in Android, as my comment to Nikola's answer show. Besides, if I understood your issue well, I definitely shouldn't have said "anyone" when all I was thinking about was the issues I face. It was just a way to emphasize that setting on the Java side wasn't my concern here. Thanks for correcting me on that. – davidcesarino Jan 04 '21 at 02:14

3 Answers3

111

You should use setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); because the documentation of the getDimension method states that it returns a Resource dimension value multiplied by the appropriate metric. which I understand to be the precalculated absolute px value.

That is, use:

tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.text_size_small));
Jose M Vidal
  • 8,816
  • 6
  • 43
  • 48
maxmc
  • 4,218
  • 1
  • 25
  • 21
  • Thank you. I guess that's what Nikola tried to say (now that I see it), but your explanation made me understand the root of the problem. – davidcesarino Aug 20 '11 at 22:00
25

Somehow this seems to fit:

XML:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="typo14">9sp</dimen>
</resources>

Java:

setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.typo14));
Alexander
  • 23,432
  • 11
  • 63
  • 73
DaRolla
  • 534
  • 1
  • 8
  • 8
  • 1
    Thanks for your code demonstrating very visually the solution. In general, this is essentially what maxmc said... using `setTextSize` with pixel units when retrieving from the resources. – davidcesarino Feb 20 '12 at 14:35
  • 1
    I had a custom control that had its `textSize` set using `sp`. When you get that value using `getDimensionPixelSize`, it get's converted to pixels. Then, when the text size is set, it defaults to `sp` (or some such) so the size was way too big. This helped a lot. – JeffRegan May 14 '14 at 19:40
2

Its a matter of sp px dpi

tv.setTextSize(14) = 14 pixels 
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • That was just an example... I won't use hard coded units in code. I updated my question to clarify. The issue is that I'm getting inconsistent font sizes from the same resource, depending if I'm retrieving in layout or in code. That's the problem I was trying to solve. – davidcesarino Jul 22 '11 at 00:34