Can you programmatically change the font of the text on the Android keyboard?

To be frank, the headline says that all this is true, I'm trying to change the input method, the KeyboardView key font. Of course .. it's not as easy as

android:keyTextFont="sans-serif" .

+3
android text fonts keyboard
source share
3 answers
 import java.lang.reflect.Field; import android.content.Context; import android.graphics.Typeface; public final class FontsOverride { public static void setDefaultFont(Context context, String staticTypefaceFieldName, String fontAssetName) { final Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName); replaceFont(staticTypefaceFieldName, regular); } protected static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) { try { final Field staticField = Typeface.class .getDeclaredField(staticTypefaceFieldName); staticField.setAccessible(true); staticField.set(null, newTypeface); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } 

add this class to your code.

 public final class Application extends android.app.Application { @Override public void onCreate() { super.onCreate(); FontsOverride.setDefaultFont(this, "DEFAULT", "fonts/GeezEdit.ttf"); FontsOverride.setDefaultFont(this, "MONOSPACE", "fonts/GeezEdit.ttf"); /*FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf"); FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf"); FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf");*/ } } 

..... here you can see some fonts / fontname added. These are external font files that you can use to override your look / keyboard labels.

Add this application name to the application name of the Android manifest file.

Example

  <application android:name=".Application" android:allowBackup="false" android:installLocation="internalOnly" android:label="@string/ime_name" android:theme="@style/AppTheme" >....... 

Now update the above font name in your style. The base theme or theme that you use in your manifest application.

Example

  <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <item name="android:typeface">monospace</item> </style> 
+2
source share

Yes, this can be done pragmatically by overriding the KeyboardView onDraw () method.

Here's an example of someone adding text above the keys: Customize the appearance of <Key>

To change the text font code will be something like this:

 public class MyKeyboardView extends KeyboardView { @Override public void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setTextAlign(Paint.Align.CENTER); Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/Hippie.otf"); //Insert your font here. paint.setTypeface(font); List<Key> keys = getKeyboard().getKeys(); for(Key key: keys) { if(key.label != null) canvas.drawText(key.label.toString(), key.x, key.y, paint); } } } 

Code not verified

0
source share

You need to create a new class that extends KeyboardView.

 package com.example.xyz; ... ... public class CustomKeyboardView extends KeyboardView{ @Override public void onDraw(Canvas canvas) { Paint mPaint = new Paint(); mPaint.setTextAlign(Paint.Align.CENTER); mPaint.setTextSize(40); mPaint.setColor(Color.BLACK); Typeface font = Typeface.createFromAsset(mContext.getAssets(),"normal_font.ttf"); mPaint.setTypeface(font); if (key.label != null) { String keyLabel = key.label.toString(); if (caps) { keyLabel = keyLabel.toUpperCase(); } canvas.drawText(keyLabel, key.x + (key.width / 2), key.y + (key.height / 2) , mPaint); } else if (key.icon != null) { key.icon.setBounds(key.x, key.y, key.x + key.width, key.y + key.height); key.icon.draw(canvas); } } } 

If the .ttf file is a font file from the Assets folder of your project.

Now in the keyboard layout (.xml) file, replace <KeyboardView/> with <com.example.xyz.CustomKeyboardView/>

0
source share

All Articles