Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistency when setting TextView font size in code and in resources

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?

like image 611
davidcesarino Avatar asked Jul 22 '11 00:07

davidcesarino


People also ask

Which method is used to change the text size of the TextView?

To use preset sizes to set up the autosizing of TextView programmatically, call the setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit) method. Provide an array of sizes and any TypedValue dimension unit for the size.

Which attribute increases or decreases the font size of TextView?

app:autoSizeMinTextSize=”10sp” using this attribute the TextView will be resized up to the size of 10sp and app:autoSizeStepGranularity=”2sp” using this attribute we are uniformly reducing the size of the TextView as 2sp when it goes out of the screen.

What is the default size of TextView in Android?

There someone discovered the default text size, for TextViews (which use TextAppearance. Small) it's 14sp.

Can we change the text in TextView?

TextView tv1 = (TextView)findViewById(R. id. textView1); tv1. setText("Hello"); setContentView(tv1);


2 Answers

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)); 
like image 98
maxmc Avatar answered Oct 14 '22 07:10

maxmc


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)); 
like image 28
DaRolla Avatar answered Oct 14 '22 07:10

DaRolla