Set text size in .xml or programmatically
Asked Answered
S

3

19

I have variable at dimens.xml

<resources>
    <dimen name="btn_text_size">12sp</dimen>    
</resources>

And i can use it in layout file:

 <TextView
           android:textSize="@dimen/btn_text_size"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/dialog_tags_complete"
/>

or programmatically

tagButton.setTextSize(c.getResources().getDimension(R.dimen.tag_text_size));

But this 2 methods give different results. I know that getDimension are based on the current DisplayMetrics associated with the resources.

But what should i do to make this 2 ways looks the same?

Sensitize answered 21/10, 2013 at 14:12 Comment(7)
Use getDimensionPixelSize().Luculent
@Luculent i tested it on my HTC One S. Yhere is no difference between getDimensionPixelSize() and getDimension() =/Sensitize
And you do realize that sp = scaled pixels, which will adjust based on the screen metrics and text-scaling factor, right? If you want to make them look the same, you'll need to use dp.Luculent
@Luculent why the same dimension look different if it set programmatically (vs .xml setting)?Sensitize
Because you're using the wrong setTextSize() method. The one you're using expects scaled pixels, while you are providing pixels. Do this instead: setTextSize( TypedValue.COMPLEX_UNIT_PX, getDimensionPixelSize( R.dimen.tag_text_size ) );Luculent
@Luculent oh, right. I'm stupid =D Tnx, it helps.Sensitize
Nothing about "being stupid" here -- it's easy to miss.Luculent
L
37

setTextSize( float ) expects a scaled pixel value. So, setTextSize( 12 ) would give you the desired result. However, getDimension() and getDimensionPixelSize() return the size in units of pixels, so you need to use the unit-typed variant of setTextSize() as follows:

setTextSize( TypedValue.COMPLEX_UNIT_PX, getDimensionPixelSize( R.dimen.tag_text_size ) );
Luculent answered 21/10, 2013 at 14:47 Comment(0)
B
3
tagButton.setTextSize(c.getResources().getDimensionPixelSize(R.dimen.tag_text_size));

this will work just fine :) You should also remember that textView has a setTextSize(int unit,float size), which should be used while setting size from code but not from xml dimen.

Burning answered 21/10, 2013 at 14:29 Comment(1)
It's still looking different from setting in .xml. Or should i set ALL size of ALL views programmatically to make no difference?Sensitize
P
0

I have currently the same thing. Did set a dimension in dimens.xml and applied it programmatically, which is 3 times that big, than when settings via xml.

I checked also:

TextView.getTextSize() = 92f
getResources().getDimension(R.dimen ...) = 92f

TextView.setTextSize(92) != TextView with size from XML, other flags like TypedValue.COMPLEX_UNIT_PX make it even bigger.

The default setTextSize does apply COMPLEX_UNIT_SP by default btw. So once again, the Android API is inconsistent, so setting programmatically only and adapt sizes, so they fit, will be my solution.

Edit: Setting text size programmatically under Galaxy Note 2 (4.4.2) vs Note 4 (5.0.1) leads to a totally different result -.-

Photograph answered 21/4, 2015 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.