Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set font scale in my own android application? [duplicate]

I want to allow user select font size.

Spare Parts do provide the feature to do that,

ActivityManagerNative.getDefault().updateConfiguration(new font size scale config)

but that is for whole all android applications. I want to add it in my application's preference and only impact for my app.

I tried below methods, it do works sometimes, but the first time I start my application, the font size still with the 1.0 scale. I do not why. I added below code in every onCreate of my activities.

Configuration config = resources.getConfiguration(); config.fontScale=1.5f; resources.updateConfiguration(config, mDisplayMetrics);

What can I do?

like image 937
virsir Avatar asked Jul 22 '11 06:07

virsir


People also ask

How do I change font size in android Apps?

To make your font size smaller or larger: On your device, open the Settings app. Search and select Font size. To change your preferred font size, move the slider left or right.

How do I stop system font size changing effects to android application?

Prevent-system-font-size-changing-effects-to-androidForms Android MainActivity. cs, override the Resources and set the configuration to default to restrict the font size effect on application. Resources. UpdateConfiguration() has been deprecated in API 25.

What is android font scale?

Some users have difficulty when they read texts that are small. They can use their device's Font size Accessibility setting to make text larger on the screen. However, this setting only affects the appearance of text if its font size was specified in units of scalable pixels (sp).


2 Answers

The code below will allow to change for the application only the font scale. On second line, the code "configuration.fontScale=(float) 1;" will have to chabge to meet your needs. The defaults are scaling with 0.15 step, having default 1.

Place it before calling setContentView(R.layout.yourlayout)

Configuration configuration = getResources().getConfiguration();
configuration.fontScale=(float) 1; //0.85 small size, 1 normal size, 1,15 big etc

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.scaledDensity = configuration.fontScale * metrics.density;
getBaseContext().getResources().updateConfiguration(configuration, metrics);

Hope this helps!

like image 137
arionargo Avatar answered Nov 02 '22 23:11

arionargo


Translated for Xamarin....

        Android.Content.Res.Configuration configuration = Resources.Configuration;
        configuration.FontScale = (float)1; //0.85 small size, 1 normal size, 1,15 big etc

        Android.Util.DisplayMetrics metrics = new DisplayMetrics();
        this.WindowManager.DefaultDisplay.GetMetrics(metrics);
        metrics.ScaledDensity = configuration.FontScale * metrics.Density;
like image 22
self.name Avatar answered Nov 03 '22 00:11

self.name