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"
.
Android 8.0 (API level 26) introduces a new feature, Fonts in XML, which lets you use fonts as resources. You can add the font file in the res/font/ folder to bundle fonts as resources. These fonts are compiled in your R file and are automatically available in Android Studio.
Tap where you can enter text. Your keyboard will appear at the bottom of the screen. the language you want to use. Swipe right and turn on the Handwriting layout.
Go to Android Settings > Languages and input > Current keyboard > Choose keyboards. You should see your Custom Keyboard on the list.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With