How to Change the Font Size in a Whole Application Programmatically, Android

How to change the Font Size in a whole Application programmatically, Android?

The probable solution would be you create a base class which extends TextView, and use this text view class as edit text. Hope you are asking for size in first screen. In any case, u set the text size in the base class. This will solve your problem.

like u create this class in package com.example and class name is BaseTextView, then in xml file instead of <TextView .../> you will write <com.example.BaseTextView ... />

Hope this helps.

Change font in whole application programmatically, based in user selection

To programmatically change the font in the whole app you can try Calligraphy library

https://github.com/InflationX/Calligraphy

Initializing the font will be in the Application class so i believe you can achieve what you are asking for

 ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf")
.setFontAttrId(R.attr.fontPath)
.build()))
.build());

you can do something like

String fontPath;
if(userSelection == 0) { //userSelection value from the server
fontPath = "fonts/FirstFont.ttf"
} else if (userSelection == 1) {
fontPath = "fonts/SecondFont.ttf"
} else {
fontPath = "fonts/ThirdFont.ttf"
}

ViewPump.init(ViewPump.builder()
.addInterceptor(new CalligraphyInterceptor(
new CalligraphyConfig.Builder()
.setDefaultFontPath(fontPath)
.setFontAttrId(R.attr.fontPath)
.build()))
.build());

I assumed you can retrieve the user selection from the server

How to change font size programmatically in Android?

Yes, setting text size is :

textView.setTextSize(20)// text size

added few more things here :)

1.If you want to set as DP

textView.setTextSize(coverPixelToDP(20));

private int coverPixelToDP (int dps) {
final float scale = this.getResources().getDisplayMetrics().density;
return (int) (dps * scale);
}

2.If you want to adjust font size automatically to fit boundaries use,

setAutoSizeTextTypeUniformWithConfiguration(int
autoSizeMinTextSize, int autoSizeMaxTextSize,
int autoSizeStepGranularity, int unit)

JAVA Version

TextView textView = new TextView(this);
textView.setText("Adjust font size for dynamic text");
//only works when width = 'match_parent', and give height
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500);

textView.setLayoutParams(p1);
textView.setAutoSizeTextTypeUniformWithConfiguration(8, 15, 1, TypedValue.COMPLEX_UNIT_DIP);

XML Version (Programatically)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent" // make sure it is match_parent
android:layout_height="500dp" //make sure you give height
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="100sp"
app:autoSizeStepGranularity="2sp" />

</LinearLayout>

Android: How to change app font size from code

It seems like I cant change app font size and I have to change font size for individual element seperateley.

How to set custom font for a whole application in Android?

Write a class

public class MyApp extends Application{
// Put the onCreate code as you obtained from the post link you reffered
}

now next thing is in AndroidManifest.xml for the application tag give name for your application class. In this case it is MyApp

<application
android:name=".MyApp"
...
>
...
</application>

So whenever the App is opened , onCreate method of MyApp class would be invoked , and the font would be set.

Update
Put font file under assets/fonts/your_font_file.ttf

Put this line under onCreate method of your application class(MyApp)

TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/your_font_file.ttf");

Source File for TypefaceUtil

public class TypefaceUtil {

/**
* Using reflection to override default typeface
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
*
* @param context to work with assets
* @param defaultFontNameToOverride for example "monospace"
* @param customFontFileNameInAssets file name of the font from assets
*/
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {

final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Map<String, Typeface> newMap = new HashMap<String, Typeface>();
newMap.put("serif", customFontTypeface);
try {
final Field staticField = Typeface.class
.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
try {
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
}

Now update your style.xml file

put the below line your style which is included for your activity in manifest file

  <item name="android:typeface">serif</item>

Hope this helps



Related Topics



Leave a reply



Submit