Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent system font-size changing effects to android application?

I recently finished developing my android application. I used sp (Scaled pixels) for all textSize. The problem is when i adjusted the system font-size, my application's font-sizes are changing. I can use dp (Device independent pixels) but it will take too long to maintain my application.

I referenced text size from this.

Is there a way to prevent system font-size changing effects to my application ?

like image 944
james Avatar asked Feb 04 '14 07:02

james


People also ask

How do you prevent system font size changing effects to Android application in xamarin forms?

In the Xamarin. Forms Android MainActivity. cs, override the Resources and set the configuration as default to restrict the font size effect on the application.

Why does my phone font size keep changing?

Answer: A: The zoom setting is on. You can turn it off via accessibility settings or double tap with three if gers and move three fingers down on the screen.

How do I make my font size permanent?

Go to Format > Font > Font. + D to open the Font dialog box. Select the font and size you want to use. Select Default, and then select Yes.


2 Answers

If you require your text to remain the same size, you'll have to use dp.

To quote the documentation:

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).

Emphasis mine.

So you're seeing the expected behaviour for using sp as your units for text size.

I don't understand what you mean about using dp taking too long to maintain your app - as far as I can tell, it'll exactly be the same amount of effort? (perhaps less, though it'll likely make it less usable for users with poor eyesight)

like image 61
Adam S Avatar answered Sep 18 '22 19:09

Adam S


I recently ran into this problem as well. Our UI didn't scale well on phones with limited screen dimensions and changing the entire UI on the off chance a user set's their Accessibility Options to "Huge" seemed silly.

I found this question on StackOverflow to be most helpful.

What I did was put the following code below in my BaseActivity (an Activity class that all my activities extend from)

public void adjustFontScale(Configuration configuration) {     if (configuration.fontScale > 1.30) {         LogUtil.log(LogUtil.WARN, TAG, "fontScale=" + configuration.fontScale); //Custom Log class, you can use Log.w         LogUtil.log(LogUtil.WARN, TAG, "font too big. scale down..."); //Custom Log class, you can use Log.w         configuration.fontScale = 1.30f;         DisplayMetrics metrics = getResources().getDisplayMetrics();         WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);         wm.getDefaultDisplay().getMetrics(metrics);         metrics.scaledDensity = configuration.fontScale * metrics.density;         getBaseContext().getResources().updateConfiguration(configuration, metrics);     } } 

And called it right after my super.onCreate() like so

adjustFontScale(getResources().getConfiguration()); 

What this code does is identify if the user set their font scale in Accessibility Settings to something greater than 1.30f (1.30f is "Large" on The Note 5, but probably varies a bit from device-to-device). If the user set their font too large ("Extra Large", "Huge"...), we scale the application only to "Large".

This allows your app to scale to a user's preferences (to a degree) without distorting your UI. Hopefully this will help others. Good luck scaling!

Other Tips

If you want certain layouts to scale with your fonts (say...a RelativeLayout that you use as a backdrop against your fonts), you can set their width/height with sp instead of the classic dp. When a user changes their font size, the layout will change accordingly with the fonts in your application. Nice little trick.

like image 27
welshk91 Avatar answered Sep 17 '22 19:09

welshk91