To be frank the title says it all really, I'm trying to change an input method service, KeyboardView key font. Of course.. it's not as simple as
android:keyTextFont="sans-serif"
.
To be frank the title says it all really, I'm trying to change an input method service, KeyboardView key font. Of course.. it's not as simple as
android:keyTextFont="sans-serif"
.
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 that the some fonts/fontname are added. These are the external font files which you can use to overrite into your keyboard view/labels.
add this Application name into the android manifest file application name
example
<application
android:name=".Application"
android:allowBackup="false"
android:installLocation="internalOnly"
android:label="@string/ime_name"
android:theme="@style/AppTheme" >.......
now update the above overrited font name into your style. base theme or the theme which you are using at your manifest application.
example
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:typeface">monospace</item>
</style>
Yes, it can be done pragmatically by overriding KeyboardView's onDraw() method.
Here is an example of someone adding text above keys: Customize the appearance of a <Key>
To change text font code would 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 tested
You have 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);
}
}
}
Where the .ttf
file is the font file from the Assets
folder of your project.
Now, in the keyboard layout (.xml) file, replace the <KeyboardView/>
with <com.example.xyz.CustomKeyboardView/>