How to Prevent System Font-Size Changing Effects to Android Application

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

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)

How to make font sizes in app to not get effected by changing Settings Font size

I finally got the solution.
Google strictly recommends to use "sp" for fonts. However, if you do not want the text to be scaled based on user font settings, use "dp". The font size will not change in a given device if you go to settings->fonts. Your UI will remain just like you designed it for.

Thanks Aswin for your inputs. And thanks to Joseph Earl for answering question at Android sp vs dp texts - what would adjust the 'scale' and what is the philosophy of support

how to prevent system font-size changing effects to nativescript-angular android application

The default unit for font size in Android is Scaled Pixels, setting it to DIP or PX will prevent auto scaling.

Tested against v6.0

import { isAndroid } from 'tns-core-modules/platform';
import { TextBase } from 'tns-core-modules/ui/text-base/text-base';

declare var android; // required if tns-platform-declarations is not installed

if (isAndroid) {
TextBase.prototype[require("tns-core-modules/ui/text-base/text-base-common").fontSizeProperty.setNative] = function (value) {
if (!this.formattedText || (typeof value !== "number")) {
if (typeof value === "number") {
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_DIP, value);
}
else {
this.nativeTextViewProtected.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);
}
}
};
}

Since you are using Angular, you may place this code in main.ts Or in app module.

Prevent system font scaling - Jetpack Compose

You can have an extension for Int or Float like this

@Composable
fun Int.scaledSp(): TextUnit {
val value: Int = this
return with(LocalDensity.current) {
val fontScale = this.fontScale
val textSize = value / fontScale
textSize.sp
}

You can add an extension parameter of Int

val Int.scaledSp:TextUnit
@Composable get() = scaledSp()

Text(text = "Hello World", fontSize = 20.scaledSp)

Disable system font size effect in my app


Disable system font size effect in my App

If you need set a fixed text size for your application, you try using the following code to implement this feature:

private void initFontScale()
{
Configuration configuration = Resources.Configuration;
configuration.FontScale = (float)1.45;
//0.85 small, 1 standard, 1.15 big,1.3 more bigger ,1.45 supper big
DisplayMetrics metrics = new DisplayMetrics();
WindowManager.DefaultDisplay.GetMetrics(metrics);
metrics.ScaledDensity = configuration.FontScale * metrics.Density;
BaseContext.Resources.UpdateConfiguration(configuration, metrics);
}

protected override void OnCreate(Bundle bundle)
{
initFontScale();
...
}

Prevent font size and zoom to the screen in my app

Since sp is just dp multiplied with the system's font scale factor, your behavior can be achieved by declaring all your text sizes in dp. You can use the same numeric values, as the default scale factor is 1.

However, this should only be used in very specific scenarios, otherwise you might reduce usability for users with visual impairments. This is why you will get warnings in your layouts when using dp for text sizes.

How can I prevent the device font size effect of a Xamarin Android app?

In your activity, add this:

public override Resources Resources {
get
{
Resources res = base.Resources;
Configuration config = new Configuration();
config.SetToDefaults();
res.UpdateConfiguration(config, res.DisplayMetrics);
return res;
}

}


Related Topics



Leave a reply



Submit