android - prevent text size change on device text size - android

I set font size 14sp for my textview in android , the problem is , when I change my phone text size , in the application it changes the size either , How can I prevent this ? How can I force text view to use the defined text size ?

When you declare a font with a size measured in sp ("scaled pixels") that measurement is supposed to be affected by the device font settings. That's fully expected.
If you want a font size that never changes with the font settings, use dp measurements instead.

Related

Text size doesn't keep aspect ratio in android studio

phone image
tablet image
I am using this line of code in android studio
btn_play_game1.setTextSize(16 * getResources().getDisplayMetrics().density );
to set the text size to a button (that has an image on it). The text is "Clear The Way" but on phone emulator looks very big and smaller on tablet.
Why the text is not as adapt to the screen size and keep the aspect ratio?
Thank you!
I think instead of multiplying your font with display density you should use the resource declared in dimens.xml file like getResources().getDimension(R.dimen.text_size_medium) this way it would not only take care of the device density but also take into account the font size set by the device user.

Set fix font size for all textview inside relative layout

I have textviews inside third party component and the parent container for this is RelativeLayout.Is there any way to programmatically set all Textview Text Size to be static I mean not change when the user uses Big fonts or big zoom from his phone settings
Scale-independent Pixels - sp means it is scaled by the user's font size preference.
Density-independent Pixels - dp is fixed size mainly used for Layouts.
If you need the same TextView size in different phone regardless of user font size preference then you should use dp.
Use this,
textView.setTextSize( TypedValue.COMPLEX_UNIT_DIP, intSize);
But sp is recommended for TextView
Try this :
text.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);

How in TextView ignore the font size configured in the phone settings (ie large font)

When i present my textview, i setup a font size of say 18. But if the user set in his phone setting to use a large font size, then my textview will be show with font much bigger than 18. How to force My text view to show with a font size exactly of 18 ?
It is because you are using 18sp. If you will use 18dp then it will not change the font size. But for font sizes "sp" is preferable so that user can increase or decrease the font size.
For more details check this link,
Difference between dp and sp
For your case, You can use this,
You can use:
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 18, getResources().getDisplayMetrics());
editText.setTextSize(pixels);
Now the value of pixels is equivalent to 18dp at the device's current screen density.
The TypedValue contains other similar methods that help in conversion.

Prevent Font Size reflection in appllication

While testing for an application I found an unknown behavior. I have defined some certain font size for TextView I am using. Now if I go in Device Setting -> and Change Display-?>Font size then it reflect in application as well. Font get tiny/large as what set in settings.
Any suggestion how can prevent from this.
You might be using sp while defining font size in xml. To prevent this behavior you can use dp instead of sp.
An sp is the same base unit, but is scaled by the user's preferred
text size (it’s a scale-independent pixel), so you should use this
measurement unit when defining text size (but never for layout sizes).
Hope this helps :)

Scale-independent Pixels font size of text in Android

Hello I am making an android application where I am giving text size is in sp as recommended.
I am setting my font size to 25sp to the TextView. Suppose here if user pick "Large" font of the device from the settings of the device and later update it to "Normal" then my application font also vary according to that.
But Is there any calculation such that I can calculate what would be size of font if it is "Large" and "Normal" ?
Screenshot of the device font settings
Sure, you can get view dimensions, but you do not really need to care in most cases. If you set size in sp then it means you are aware what that unit means and you expect this behaviour. If you do not like fonts size being changed, simply use dp

Categories

Resources